Override DateTime.ToString to use "o" instead of "G"
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.
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
精彩评论