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

发布于 2024-07-26 03:15:32 字数 1113 浏览 5 评论 0 原文

根据 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";
      }
}

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));
    }
}

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));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟酉 2024-08-02 03:15:32

是的,它确实。 正如其他人所说,它只包含数值和字符串文字(例如“T”和“:”),不会因区域或文化设置而改变。

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.

你又不是我 2024-08-02 03:15:32

是的。 打破该模式,它只是数字属性,没有任何引用例如其中的月份或日期名称

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文