Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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));
    }
}
like image 796
Peter Gfader Avatar asked Jun 29 '09 12:06

Peter Gfader


People also ask

How do I change the date format in ToString?

To do this, you use the "MM/yyyy" format string. The format string uses the current culture's date separator. Getting a string that contains the date and time in a specific format. For example, the "MM/dd/yyyyHH:mm" format string displays the date and time string in a fixed format such as "19//03//2013 18:06".

Is DateTime a string format?

A date and time format string defines the text representation of a DateTime or DateTimeOffset value that results from a formatting operation. It can also define the representation of a date and time value that is required in a parsing operation in order to successfully convert the string to a date and time.

What is the default format of DateTime in C#?

The custom format string is "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'".


1 Answers

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

like image 96
Dan F Avatar answered Oct 13 '22 00:10

Dan F