无论文化如何,在标签中设置日期格式
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您尝试格式化日期两次:
调用
删除对
ToShortDateString
Update 的或者更好:
要真正确定区域性不会影响事物,您可以显式指定区域性:
You're trying to format the date twice:
Remove the call to
ToShortDateString
Update
Or better yet:
To make really certain that the culture isn't impacting things, you could specify the culture explicitly:
我认为如果你将: 替换
为:
它应该有效
I think if you replace:
with:
it should work
您的问题在于:
您尝试将日期格式化为具有特定格式的字符串,但您已经使用
ToShortDateString
(使用当前区域性)将日期转换为字符串。您在string.Format
中指定的格式对于字符串没有意义...相反,您应该像这样格式化日期:
顺便说一下,请注意“MM”部分而不是“mm” : mm 表示分钟,这证明您指定的格式没有在您当前的代码中考虑在内
Your problem is here:
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 instring.Format
doesn't make sense for a string...Instead you should format the date like this:
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
将
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())
withBirthDate = 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
你为什么不做这条线
进入 tbBirthDate.Text = cust.BirthDate.ToString("dd-MM-yyyy");
当然,这不会考虑任何文化信息!
Why don't you make this line
into
tbBirthDate.Text = cust.BirthDate.ToString("dd-MM-yyyy")
;This wont take into account any cultureinfo ofcourse!