我有一个这种格式的字符串:“20111027”,即通用格式:“yyyyMMdd”。
如何将其转换为具有 timezone GMT 的 DateTime?
此代码进行一些转换,但不清楚将使用哪个时区:
DateTime date = DateTime.ParseExact(dateString, "yyyyMMdd",
CultureInfo.InvariantCulture);
最佳答案
使用 AssumeUniversal
的 DateTimeStyles
:
DateTime date = DateTime.ParseExact(dateString, "yyyyMMdd",
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal);
来自docs of DateTimeStyles.AssumeUniversal
:
If no time zone is specified in the parsed string, the string is assumed to denote a UTC.
听起来正是你想要的:)
(或者,您可以使用 Noda Time 并将其解析为 LocalDate
。毕竟它只表示日期,那么为什么要使用关心时间和时区的类型呢?:)
关于c# - 将字符串 "20111027"转换为时区为 GMT 的 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7921626/