I am trying to set the DateTime
format to 24 hours. Originally I have a string with a 12 hour representation. All solutions I have found are converting DateTime
to string.
string dateString = "Mon 16 Jun 8:30 AM 2008"; // <-- Valid
string format = "dd/MM/yyyy HH:mm";
DateTime dateTime;
if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind, out dateTime))
{
DateTime dateIn24 = dateTime;// dateIn24 should be in 24 hour format
}
Is there anything we can do in web.config
? Like the following:
<globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB"/>
i got the answer
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); if its en-US 12 hour format by default it will be en-US based on system date time settings
The DateTime instance only contains the information, and the Hour property is an integer from 0 to 23, according to MSDN documentation:
Property Value
Type : System.Int32
The hour component, expressed as a value between 0 and 23.
https://msdn.microsoft.com/library/system.datetime.hour(v=vs.110).aspx
If you're talking about formatting it, then you need to convert it to a string, like mentioned in the comments.
As far as I know, the 24-hour format is neither not a matter of how the DateTime
is parsed, nor saved, but rather of the formatting. Given that you enter the if
-block (I have not been successful on parsing youre dateString
in LinqPad) you can obtain a correctly formatted date string with
var dateStringWith24Hours = dateTime.ToString(dateString);
since the HH
in your format string means that you'd like to format the hours as 24 hours.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.