无论文化如何,在标签中设置日期格式

发布于 2024-12-12 02:09:45 字数 2064 浏览 0 评论 0原文

在我的 ascx 页面中,我有:

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

此 Birtdate 是从 cs 文件填充的:

        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;
        }

现在,当在本地运行时,我得到一个像 26-10-2011 这样的日期,但是当放在我的服务器上时,我得到 10/26/2011 我如何强制日期显示为 26-10-2011 ?

In my ascx page i have :

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

This Birtdate is filled from the cs file :

        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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

春花秋月 2024-12-19 02:09:45

您尝试格式化日期两次:

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

调用

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

删除对 ToShortDateString Update 的

或者更好:

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

要真正确定区域性不会影响事物,您可以显式指定区域性:

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

You're trying to format the date twice:

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

Remove the call to ToShortDateString

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

Update

Or better yet:

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

To make really certain that the culture isn't impacting things, you could specify the culture explicitly:

c.BirthDate.ToString("dd-MM-yyyy", 
    CultureInfo.GetCultureInfo("en-US").DateTimeFormat)
或十年 2024-12-19 02:09:45

我认为如果你将: 替换

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

为:

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

它应该有效

I think if you replace:

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

with:

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

it should work

—━☆沉默づ 2024-12-19 02:09:45

您的问题在于:

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

您尝试将日期格式化为具有特定格式的字符串,但您已经使用 ToShortDateString (使用当前区域性)将日期转换为字符串。您在 string.Format 中指定的格式对于字符串没有意义...

相反,您应该像这样格式化日期:

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

顺便说一下,请注意“MM”部分而不是“mm” : mm 表示分钟,这证明您指定的格式没有在您当前的代码中考虑在内

Your problem is here:

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...

Instead you should format the date like this:

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

By the way, note the "MM" part instead of "mm": mm is for minutes, which proves the format you specified wasn't taken into account in your current code

伪心 2024-12-19 02:09:45

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

第一种形式首先获取日期,然后是短日期字符串的本地形式,然后对其进行格式化但不执行任何操作。第二个获取日期,然后对其进行格式化,将其放入 dd-mm-yyyy

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

The first form first gets the date, then the local-form of the short date string, then does a format on it that does nothing. The second gets the date and then does a format on it that puts it into dd-mm-yyyy

戒ㄋ 2024-12-19 02:09:45

你为什么不做这条线

 tbBirthDate.Text = cust.BirthDate;

进入 tbBirthDate.Text = cust.BirthDate.ToString("dd-MM-yyyy");

当然,这不会考虑任何文化信息!

Why don't you make this line

 tbBirthDate.Text = cust.BirthDate;

into tbBirthDate.Text = cust.BirthDate.ToString("dd-MM-yyyy");

This wont take into account any cultureinfo ofcourse!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文