DateTime.ToString("s")是否始终返回相同的格式?

Pet*_*der 8 datetime cultureinfo tostring regional

根据MSDN on DateTime.ToString ToString("s")应始终以可排序的XML Schema样式格式的格式返回字符串,例如:2008-10-01T17:04:32.0000000

在Reflector中,我在DateTimeFormatInfo中找到了这个模式.

public string SortableDateTimePattern
{
      get
      {
            return "yyyy'-'MM'-'dd'T'HH':'mm':'ss";
      }
}
Run Code Online (Sandbox Code Playgroud)

DateTime.ToString("s")是否始终返回此格式的字符串?
无论文化,地区,......


是的,它
代码测试

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

sam*_*son 16

是的,它确实.正如其他人所说,它只包含数值和字符串文字(例如'T'和':'),没有任何区域或文化设置改变.


Dan*_*n F 12

是的.打破这种模式,它只是数字属性,没有像月份或日期名称那样的引用.

yyyy - 4位数日期
MM - 2位数月份,前导零
dd - 2位数日,前导零
T - 文字T
HH - 2位小时,前导零,24小时格式
mm - 2位分钟,前导零
ss - 2位数秒,前导零