tags:

views:

51

answers:

6

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?

+4  A: 

You may also try a custom format to avoid culture specific confusions:

DateTime.Now.ToString("hh:mm tt")
Darin Dimitrov
+1  A: 

Yeah, this depends on your Locale. What is the value of System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern in your application?

See MSDN Link

palswim
+4  A: 

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

Martin
+1  A: 

You can set the thread's culture info and this will then be used by the ToShortTimeString() method. But understand that this will effect all code running in that thread.

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
Jesper Palm
+1  A: 

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.

AllenG
A: 

The function uses the users default patterns. They can be changed in the Control panel. Check out first tab in the 'Region and Language' Settings. Change the Short time pattern to a pattern that like 'h:mm tt' and you're done.

jerleth