我从用户那里获取日期数据。该数据是一个日期(例如 4/23/2011)和一个小时(0 - 23),代表时间。用户选择的这个日期/时间是本地时间。
我需要将其转换为 UTC 日期时间。我有他们的 GMTOffset 作为他们的位置。我该怎么做?
最佳答案
您应该使用 DateTimeOffset
structure ,特别是 constructor that takes the DateTime
and the TimeSpan
that represents the offset .
从那里开始,与 UTC 之间的转换变得轻而易举,因为偏移量嵌入到结构中并且不依赖于本地系统设置。
请注意,即使没有普遍遵守,它也是 recommended to work with DateTimeOffset
most of the time, as opposed to DateTime
(请参阅标题为“DateTimeOffset 结构”的部分下的注释)。
关于C#/.NET 日期 : From local to UTC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5781677/