【发布时间】:2020-05-26 20:31:26
【问题描述】:
我在使用 DateTime 的“U”格式说明符时遇到了奇怪的行为,如以下通过测试所示:
DateTime tim = new DateTime(2020, 03, 29, 00, 59, 59);
//test 1
Assert.Equal("2020-03-29 00:59:59Z", tim.ToString("u", CultureInfo.GetCultureInfo("en-GB")));
//test 2
Assert.Equal("28 March 2020 23:59:59", tim.ToString("U", CultureInfo.GetCultureInfo("en-GB")));
//test 3
Assert.Equal("Saturday, March 28, 2020 11:59:59 PM", tim.ToString("U", CultureInfo.GetCultureInfo("en-US")));
//test 4
Assert.Equal("2020-03-29 00:59:59Z", timA.ToString("u", CultureInfo.GetCultureInfo("en-US")));
Test1 和 Test4 都生成包含日期和时间的预期字符串以显示给用户; 2020-03-29 00:59:59。但是,Test2 和 3 显示一个字符串,显示时间提前一小时; 2020 年 3 月 28 日 23:59:59。为什么格式说明符会改变值?
- “u”(通用可排序日期/时间;标准格式字符串)UniversalSortableDateTimePattern,用于定义符合协调通用时间的 ISO 8601 标准的结果字符串。该属性是只读的。
- “U”(通用完整日期/时间;标准格式字符串)FullDateTimePattern,用于定义结果字符串的整体格式。
为什么格式说明符会改变值?此外,为什么要改变一小时呢?
【问题讨论】: