将unix日期时间戳转换为javascript中特定时区的时间
我想将unix时间戳转换为特定时区的时间。
I want to convert unix timestamp to time for a particular timezone.
比如说unix时间戳是 - 1446960600
我要转换这个时间戳到美国/洛杉矶?
say for example unix time stamp is - 1446960600 I want to convert this timestamp to America/Los Angeles ?
您可以将unix时间戳整数转换为像这样的Javascript日期
You can convert a unix timestamp integer into a Javascript date like this
var date = new Date(1446960600 * 1000);
Unix时间戳是UTC时区,但是没有一种简单的方法来进行时区更改Javascript。
Unix timestamps are in the UTC timezone, but there isn't an easy way to do timezone changes in Javascript.
我建议您使用时间库,例如 momentjs 。它可以处理像夏令时这样的边缘情况,如果你自己这样做就必须考虑到它。
I recommend you use a time library like momentjs. It handles edge cases like daylight savings that you would have to take into account if you do it yourself.
以下是另一个答案的示例代码:
Here's some sample code from another answer:
function toTimeZone(time, zone) {
var format = 'YYYY/MM/DD HH:mm:ss ZZ';
return moment(time, format).tz(zone).format(format);
}
https://stackoverflow.com/a/18612568/1031569
您可以使用 getTimezoneOffset()
DateTime计算时区变化的方法,但要了解这一点,不会考虑日光节约或闰年变化。
You can use the getTimezoneOffset()
method of a DateTime to calculate the timezone change, but understand this won't take into account daylights saving or leap year changes.