Tuesday, September 25, 2007

Custom DateTime Format Specifiers problem solved

Just wanted to share this problem that can show up if you are developing using for for example VB.NET or C# and want to force a custom date and time format.

The problem can show up with format specifiers %t that represents the first character of the A.M./P.M. designator or tt that represents the A.M./P.M. designator.

Notice how PM is missing if the culture is Swedish.

System.Threading.Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo("sv-SE")
Dim MyDate As New DateTime(2007, 9, 12, 17, 30, 0)
Dim MyString As String = MyDate.ToString("MMM dd 'at' h:mm tt")
MyString returns "sep 12 at 5:30 "

The solution is to set the culture to one that supports AM and PM like en-US and then you can restore it to what it was before.

System.Threading.Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo("en-US")
Dim MyDate As New DateTime(2007, 9, 12, 17, 30, 0)
Dim MyString As String = MyDate.ToString("MMM dd 'at' h:mm tt")
MyString returns "Sep 12 at 5:30 PM"

No comments:

Post a Comment

Subscribe to the comments feed

Some of the latest blog posts

Subscribe to RSS headline updates from:
Powered by FeedBurner

Contact Us | About JTB World | Subscribe to this blog
JTB World's website | Website General Terms of Use | Privacy Policy
^ Top of page

© 2004- JTB World. All rights reserved.