I need the time in AM/PM format in an ASP.NET MVC 3 application, and I am using the following statement:
@Model.ReportDate.ToString("hh:mm tt")
But 开发者_JAVA技巧it is returning 6:45 instead of 6:45 PM. How do I fix this?
The format strings are culture/locale dependent so it may be using the settings from your server (e.g. 24 hour time). Try this:
@Model.ReportDate.ToString("hh:mm tt",
System.Globalization.CultureInfo.InvariantCulture)
If this (or something similar) solves your problem, I suggest setting the desired culture in global.asax
so your entire app uses the culture you expect.
Depends on the culture:
For cultures that do not use an AM (or PM) designator, this property returns an empty string.
try to use the CultureInfo.InvariantCulture
ToString("hh:mm tt", CultureInfo.InvariantCulture )
It is associated with the English language but not with any country/region.
精彩评论