« A Day in the Zenzo - Mischief and Mayhem... | Main | Hanselminutes Podcast 29 - Dynamic vs Co... »

Had an interesting back and forth with Tom Wayson today. He wanted to be able to "modify the default behavior of DateTime.ToString()."

So, pushing aside issues of localization and the questions of "why would you want to do that?" let's look at the problem.

He has an intranet application and doesn't need to localize it. He wants to 'enforce' a specific date/time format and wants to avoid writing DateTime.Now.ToString("MM/dd/yy HH:mm:ss") everytime. He also doesn't want to explicitly set the system-wide settings in Control Panel | Regional Settings.

He noted that the output of DateTime.Now.ToString on a standard en-us machine in the states gave this output:

8/15/2006 3:27:27 PM

It looks like the output of ToString is the combination of the DateTimeFormatInfo.ShortDatePattern and DateTimeFormatInfo.ShortTimePattern. So, he: 

Imports System

Imports System.Globalization

 

Public Module MyModule

    Sub Main()

        Dim customCulture As CultureInfo = New CultureInfo("en-US")

        customCulture.DateTimeFormat.ShortDatePattern = "MM/dd/yy"

        'HH means 24 hour time, while hh means 12 hour time
        customCulture.DateTimeFormat.ShortTimePattern = "HH:mm:ss"

 

        System.Threading.Thread.CurrentThread.CurrentCulture = customCulture

        System.Threading.Thread.CurrentThread.CurrentUICulture = customCulture

 

        WL(DateTime.Now.ToString())

        WL(DateTime.Now.ToShortDateString())

        WL(DateTime.Now.ToShortTimeString())

    End Sub

 

    Sub WL(ByVal text As Object)

        Console.WriteLine(text)

    End Sub

End Module 

But the output was still 12 hour time:

8/15/06 3:28:34 PM

Ah...a little Reflectoring shows us that the default format string for System.DateTime is "G" as in System.DateTime.ToString("G") where G is one of the presets.

From PowerShell we see:

PS>C:\Documents and Settings\shanselm\Desktop
[DateTime]::Now.ToString("g")
8/15/2006 3:28 PM
PS>C:\Documents and Settings\shanselm\Desktop
[DateTime]::Now.ToString("G")
8/15/2006 3:28:02 PM
PS>C:\Documents and Settings\shanselm\Desktop
[DateTime]::Now.ToString()
8/15/2006 3:28:15 PM

So we add:

Imports System

Imports System.Globalization

 

Public Module MyModule

    Sub Main()

        Dim customCulture As CultureInfo = New CultureInfo("en-US")

        customCulture.DateTimeFormat.ShortDatePattern = "MM/dd/yy"

        'HH means 24 hour time, while hh means 12 hour time
        customCulture.DateTimeFormat.ShortTimePattern = "HH:mm:ss"       
        customCulture.DateTimeFormat.LongTimePattern = "HH:mm:ss"

 

        System.Threading.Thread.CurrentThread.CurrentCulture = customCulture

        System.Threading.Thread.CurrentThread.CurrentUICulture = customCulture

 

        WL(DateTime.Now.ToString())

        WL(DateTime.Now.ToShortDateString())

        WL(DateTime.Now.ToShortTimeString())

    End Sub

 

    Sub WL(ByVal text As Object)

        Console.WriteLine(text)

    End Sub

End Module 

And gets the output he expects, indicating that "G" is the combination of a ShortDate and a LongTime.

8/15/2006 15:28:57

Tracked by:
"Interesting Finds: August 15, 2006" (Jason Haley) [Trackback]
"Enabling Evil - Overriding System.DateTime's default ToString" (DotNetKicks.com... [Trackback]
"Date (and time) formats in ASP.Net" (The glamorous world of software developmen... [Trackback]
"Gets Me Every Time" (LifeCycle Solutions, LLC - Weblog) [Trackback]
http://blog.lifecycle-solutions.com/Gets+Me+Every+Time.aspx [Pingback]


Tuesday, August 15, 2006 6:16:07 PM (Pacific Standard Time, UTC-08:00)
There is an existing DateTimeFormatInfo, DateTimeFormatInfo.InvariantCulture, that has a similar format. If you want to use it (and not change the rest of your CurrentCulture):

CultureInfo customCulture = new CultureInfo(Thread.CurrentThread.CurrentCulture.Name);
customCulture.DateTimeFormat = DateTimeFormatInfo.InvariantInfo;

Thread.CurrentThread.CurrentCulture = customCulture;

string sNow = DateTime.Now.ToString();

This will output the same string that you have. But honestly, you should pass DateTimeFormatInfo.InvariantInfo to ToString() explicitly, and then you don't have to go about mangling the thread's cultureinfo. While you're at it, this thing is going across the wire? Maybe you ought to use UTC and a format that round trips.

string sNow = DateTime.UtcNow.ToString("u", DateTimeFormatInfo.InvariantInfo);

it comes out the other side...

DateTime dNow = DateTime.Parse(sNow, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.RoundtripKind);
DateTime dLocal = dNow.ToLocalTime();

c
chris
Tuesday, August 15, 2006 11:39:33 PM (Pacific Standard Time, UTC-08:00)
Dude, you're preaching to the converted. That's why the post was called "Enabling Evil" and had the disclaimers at the top explaining the constraint that the guy had - he wanted to change the behavior of EXISTING ToString() calls.
Scott Hanselman
Wednesday, August 16, 2006 6:06:19 AM (Pacific Standard Time, UTC-08:00)
Glad to hear I'm not the only developer being forced to do this. Our customer comes with alot of red tape. They want their dates formatted 'dd MMM yyyy'. At first they mandated that all dates be run through a SQL user-defined function called 'PrepareDate(date)' before being displayed. I was able to convince them that a round trip to their SQL server for each date was a bad idea, and we compromised on a similar solution, overriding the default short date format as above.

We're now migrating to 2.0, though, and may be able to sneak a proper fix in. So what would be the "right" way to do this? A custom culture? It will need to work with databinding- ie setting the format string to {0:d} or similar...
Wednesday, August 16, 2006 7:16:21 AM (Pacific Standard Time, UTC-08:00)
Scott, I find your comments about changing default behavior ('evil') interesting in light of our email conversation a week or two ago, regarding the of Object.prototype in JavaScript . . . (You know, the one where you asked me "Why isn't it a good thing? It works...").

;)

Wednesday, August 16, 2006 7:17:07 AM (Pacific Standard Time, UTC-08:00)
Last comment should have read like this:


Scott, I find your comments about changing default behavior ('evil') interesting in light of our email conversation a week or two ago, regarding the hacking of Object.prototype in JavaScript . . . (You know, the one where you asked me "Why isn't it a good thing? It works...").

;)

Comments are closed.

Contact

Sponsors

Hosting By

Hot Topics

Tags

Calendar

<November 2009>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

November, 2009 (2)
October, 2009 (19)
September, 2009 (11)
August, 2009 (12)
July, 2009 (21)
June, 2009 (26)
May, 2009 (16)
April, 2009 (13)
March, 2009 (17)
February, 2009 (17)
January, 2009 (18)
December, 2008 (32)
November, 2008 (17)
October, 2008 (22)
September, 2008 (16)
August, 2008 (14)
July, 2008 (25)
June, 2008 (19)
May, 2008 (17)
April, 2008 (17)
March, 2008 (26)
February, 2008 (21)
January, 2008 (28)
December, 2007 (19)
November, 2007 (17)
October, 2007 (31)
September, 2007 (39)
August, 2007 (37)
July, 2007 (43)
June, 2007 (37)
May, 2007 (32)
April, 2007 (38)
March, 2007 (29)
February, 2007 (46)
January, 2007 (31)
December, 2006 (27)
November, 2006 (31)
October, 2006 (32)
September, 2006 (39)
August, 2006 (34)
July, 2006 (40)
June, 2006 (18)
May, 2006 (31)
April, 2006 (34)
March, 2006 (30)
February, 2006 (38)
January, 2006 (44)
December, 2005 (19)
November, 2005 (34)
October, 2005 (24)
September, 2005 (37)
August, 2005 (20)
July, 2005 (24)
June, 2005 (33)
May, 2005 (16)
April, 2005 (22)
March, 2005 (34)
February, 2005 (15)
January, 2005 (37)
December, 2004 (28)
November, 2004 (30)
October, 2004 (34)
September, 2004 (22)
August, 2004 (34)
July, 2004 (18)
June, 2004 (64)
May, 2004 (49)
April, 2004 (21)
March, 2004 (29)
February, 2004 (29)
January, 2004 (36)
December, 2003 (25)
November, 2003 (24)
October, 2003 (59)
September, 2003 (42)
August, 2003 (24)
July, 2003 (44)
June, 2003 (29)
May, 2003 (21)
April, 2003 (30)
March, 2003 (27)
February, 2003 (47)
January, 2003 (50)
December, 2002 (31)
November, 2002 (38)
October, 2002 (44)
September, 2002 (15)
May, 2002 (2)
April, 2002 (4)

Google Ads