如何将DateTime转换为不同的时区?

问题描述:

如何将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.