English 中文(简体)
1. 不论文化为何,在标签上注明日期
原标题:Format a date in label regardless of culture

我的 as页一有:

    <tr>
        <td class="lbl">Geboortedatum</td>
        <td class="lbl">:</td>
        <td class="data">
            <asp:Label ID="tbBirthDate"  CssClass="details" runat="server" />
        </td>
    </tr>

1. 本附件从卷宗中填满:

        var cust = (from c in db.tbCustomers
                    join s in db.tbStreets on new { c.StreetId, c.PostCode } equals new { s.StreetId, s.PostCode }
                    join p in db.tbPostalAreas on s.PostCode equals p.PostCode
                    where c.Id == customerId
                    select new
                    {
                        FirstNames = c.FirstNames,
                        MiddleName = c.MiddleName,
                        LastName = c.LastName,
                        BirthDate = string.Format("{0:dd-mm-yyyy}", c.BirthDate.Value.ToShortDateString()),
                        StreetName = s.Street,
                        HouseNumber = c.HouseNumber,
                        PostCode = c.PostCode,
                        PostCodeLetters = c.StreetId,
                        City = p.City,
                        Email = c.Email,
                        Mobile = c.PhoneMobile,
                        PickUpAddress = c.PickupAddress

                    }).SingleOrDefault();

        if (cust != null)
        {
            tbFirstName.Text = cust.FirstNames;
            tbLastName.Text = (cust.MiddleName != null) ? cust.MiddleName + " " + cust.LastName : cust.LastName;
            tbBirthDate.Text = cust.BirthDate;
            tbStreetName.Text = cust.StreetName + " " + cust.HouseNumber;
            tbPostCode.Text = cust.PostCode + " " + cust.PostCodeLetters;
            tbCity.Text = cust.City;
            tbEmail.Text = cust.Email;
            tbMobile.Text = cust.Mobile;
            tbPickupAddress.Text = cust.PickUpAddress;
        }

Now when running locally i get a date like 26-10-2011, but when placed on my server i get 10/26/2011 How can i force the date to be shown as 26-10-2011 ?

问题回答

You re trying to format the date twice:

string.Format("{0:dd-mm-yyyy}", c.BirthDate.Value.ToShortDateString())

取消对<代码>的呼吁 ToShortDateString

string.Format("{0:dd-mm-yyyy}", c.BirthDate.Value)

Update

Or better yet:

c.BirthDate.Value.ToString("dd-MM-yyyy")

为了真正确定文化会对事物产生影响,你可以明确指明文化:

c.BirthDate.ToString("dd-MM-yyyy", 
    CultureInfo.GetCultureInfo("en-US").DateTimeFormat)

我认为,你可以取代:

BirthDate = string.Format("{0:dd-mm-yyyy}", c.BirthDate.Value.ToShortDateString()),

with:

BirthDate = string.Format("{0:dd-mm-yyyy}", c.BirthDate.Value).

它的工作

你的问题是:

string.Format("{0:dd-mm-yyyy}", c.BirthDate.Value.ToShortDateString())

You re trying to format the date as a string with a specific format, but you have already transformed the date into a string with ToShortDateString (using the current culture). The format you specified in string.Format doesn t make sense for a string...

请将此日期改为:

c.BirthDate.ToString("dd-MM-YYYY");

顺便提一下“MM”部分,而不是“mm”:毫米为分钟,这证明了你在现行法典中具体提到的形式。

BirthDate = string.Format ("{0:ddd-mm-yyyy}”, c.BirthDate.Value.ToShortDateString () with BirthDate = string.Format("{0:d-mm-yyyyyyyyy}”, c.BirthDate.Value)

第一种形式先是日期,然后是短日期的当地格式,然后是形式,没有。 第二种日期,然后采用一种格式,将其变为mm-mm

为什么不要让你这样做

 tbBirthDate.Text = cust.BirthDate;

tbBirthDate.Text = cust.BirthDate.ToString(“d-MM-yyyyyyyy”;

这一胜利考虑到了任何文化。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签