如何将 DateTime 转换为不同的时区? [英] How to convert DateTime into different timezones?
问题描述
如何将 DateTime 转换为不同的时区?DateTime 类有两个方法 .toLocal() 和 .toUtc().但是如果我想在另一个时区显示时间.我该怎么做?
How to convert DateTime into different timezones? The DateTime class has two methods .toLocal() and .toUtc(). But if I want to display time in another time zone. How can I do it?
推荐答案
DateTime
不包含时区信息,因此您无法在特定时区创建 DateTime
只有您系统的时区和 UTC 可用.
DateTime
doesn't contain timezone information therefore you can't create a DateTime
in a specific timezone only the timezone of your system and UTC are available.
您可以将 DateTime 包装在自定义类中,并将时区信息添加到包装器中.您还需要每个时区的偏移量表,然后从 UTC 日期添加/减去偏移量.
You can wrap the DateTime in a custom class and add timezone information to the wrapper. You also need a table of offsets for each timezone and then add/substract the offset from the UTC date.
这篇关于如何将 DateTime 转换为不同的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!