How to convert DateTime into different timezones?

前端 未结 4 1526
囚心锁ツ
囚心锁ツ 2021-01-02 04:02

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 d

4条回答
  •  离开以前
    2021-01-02 04:24

    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.

    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.

提交回复
热议问题