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

2

12

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));
    }
}
Worship answered 29/6, 2009 at 12:24 Comment(1)
how is this question not yet closed?Miscellanea
H
19

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.

Hagiolatry answered 29/6, 2009 at 12:25 Comment(0)
F
14

Yep. Breaking that pattern down, it's only numeric properties, there's no reference to anything like month or day names in there.

yyyy - 4 digit date
MM - 2 digit month, with leading zero
dd - 2 digit day, with leading zero
T - a literal T
HH - 2 digit hour, with leading zero, 24 hour format
mm - 2 digit minute, with leading zero
ss - 2 digit second, with leading zero

Facelift answered 29/6, 2009 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.