在我的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?
答案 0 :(得分:2)
您正尝试将日期格式化两次:
string.Format("{0:dd-mm-yyyy}", c.BirthDate.Value.ToShortDateString())
移除对ToShortDateString
string.Format("{0:dd-mm-yyyy}", c.BirthDate.Value)
或者更好:
c.BirthDate.Value.ToString("dd-MM-yyyy")
为了确保文化不会影响事物,您可以明确指定文化:
c.BirthDate.ToString("dd-MM-yyyy",
CultureInfo.GetCultureInfo("en-US").DateTimeFormat)
答案 1 :(得分:1)
我想如果你替换:
BirthDate = string.Format("{0:dd-mm-yyyy}", c.BirthDate.Value.ToShortDateString()),
使用:
BirthDate = string.Format("{0:dd-mm-yyyy}", c.BirthDate.Value).
它应该有用
答案 2 :(得分:0)
你的问题在这里:
string.Format("{0:dd-mm-yyyy}", c.BirthDate.Value.ToShortDateString())
您尝试将日期格式化为具有特定格式的字符串,但您已将日期转换为ToShortDateString
的字符串(使用当前文化)。您在string.Format
中指定的格式对字符串没有意义......
相反,你应该像这样格式化日期:
c.BirthDate.ToString("dd-MM-YYYY");
顺便说一句,请注意“MM”部分而不是“mm”:mm表示分钟,这证明您指定的格式未在当前代码中考虑
答案 3 :(得分:0)
将BirthDate = string.Format("{0:dd-mm-yyyy}", c.BirthDate.Value.ToShortDateString())
替换为BirthDate = string.Format("{0:dd-mm-yyyy}", c.BirthDate.Value)
。
第一个表单首先获取日期,然后是短日期字符串的本地形式,然后在其上执行一个什么都不做的格式。第二个获取日期,然后在其上执行一个格式,将其放入dd-mm-yyyy
答案 4 :(得分:0)
你为什么不这一行?
tbBirthDate.Text = cust.BirthDate;
进入tbBirthDate.Text = cust.BirthDate.ToString("dd-MM-yyyy")
;
这不会考虑任何cultureinfo ofcourse!