Does DateTime.ToString("s") return always same format?

Peter Gfader picture Peter Gfader · Jun 29, 2009 · Viewed 16.8k times · Source

According to MSDN on DateTime.ToString ToString("s") should always return string in the format of the sortable XML Schema style formatting, e.g.: 2008-10-01T17:04:32.0000000

In Reflector I came to this pattern inside DateTimeFormatInfo.

public string SortableDateTimePattern
{
      get
      {
            return "yyyy'-'MM'-'dd'T'HH':'mm':'ss";
      }
}

Does DateTime.ToString("s") return always a string in this format?
Regardless the Culture, Region, ...


Yes it does
Code to test that

var dateTime = DateTime.Now;
var originialString = dateTime.ToString("s");
string testString;

foreach (var c in System.Globalization.CultureInfo.GetCultures(CultureTypes.AllCultures))
{
    Thread.CurrentThread.CurrentUICulture = c;
    if (c.IsNeutralCulture == false)
    {
        Thread.CurrentThread.CurrentCulture = c;
    }

    testString = dateTime.ToString("s");
    Console.WriteLine("{0} ", testString);
    if (originialString != testString)
    {
        throw new ApplicationException(string.Format("ToString(s) is returning something different for {0} " , c));
    }
}

Answer

samjudson picture samjudson · Jun 29, 2009

Yes it does. As others have said it only contains numeric values and string literals (e.g. 'T' and ':'), nothing that is altered by region or culture settings.