I've run into a problem that's driving me crazy. In my application (ASP.NET MVC2 /.NET4), I simply running this:
DateTime.Now.ToShortTimeString()
All the examples I've seen indicate I should get something like: 12:32 PM
, however I'm getting 12:32
without the AM/PM.
I launched LinqPad 4 to see if I could replicate this. Instead, it returns 12:32 PM
correctly.
What the hell?
You may also try a custom format to avoid culture specific confusions:
DateTime.Now.ToString("hh:mm tt")
KBrimington looks to be correct:
The string returned by the ToShortTimeString method is culture-sensitive. It reflects the pattern defined by the current culture's DateTimeFormatInfo object. For example, for the en-US culture, the standard short time pattern is "h:mm tt"; for the de-DE culture, it is "HH:mm"; for the ja-JP culture, it is "H:mm". The specific format string on a particular computer can also be customized so that it differs from the standard short time format string.
From MSDN
If you don't want to mess with the Culture for your whole thread/application, try this:
CultureInfo ci = new CultureInfo("en-US");
string formatedDate = DateTime.Now.ToString("t", ci);
You can find the list of DateTime Format strings here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With