如何将 DateTime 转换为不同的时区? [英] How to convert DateTime into different timezones?

查看:43
本文介绍了如何将 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.

这篇关于如何将 DateTime 转换为不同的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆