tags:

views:

205

answers:

1

Is there any way in .Net I can get the default format used by DateTime.ToString() to change from ToString("G") to ToString("o")?

I don't have the option of using a different overload it has to be ToString().

Update: I found a way to do what I need using DateTimeOffset but it was very useful to see Gonzalo's solution.

+2  A: 

This is the closest I could get. The only problem is that there's a space between the date and the T:

using System;
using System.Threading;
using System.Globalization;

class Test {
    static void Main ()
    {
     Console.WriteLine (DateTime.Now.ToString ("o"));
     CultureInfo culture = (CultureInfo) CultureInfo.CurrentCulture.Clone();
     culture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
     culture.DateTimeFormat.LongTimePattern = "THH:mm:ss.fffffffzzz";
     Thread.CurrentThread.CurrentCulture = culture;
     Console.WriteLine (DateTime.Now);
    }
}

The output was:

2009-11-23T00:32:53.5291030-05:00
2009-11-23 T00:32:53.6493060-05:00

Gonzalo
I briefly looked at that option but wasn't clever enough to work it out :). I don't fancy having to do that on every thread in my app though...
sipwiz