将unix日期时间戳转换为javascript中特定时区的时间 [英] Converting unix date-timestamp to time for a particular timezone in javascript

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

这篇关于将unix日期时间戳转换为javascript中特定时区的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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