[英]Converting unix date-timestamp to time for a particular timezone in javascript
I want to convert unix timestamp to time for a particular timezone. 我想将unix时间戳转换为特定时区的时间。
say for example unix time stamp is - 1446960600 I want to convert this timestamp to America/Los Angeles ? 例如,unix时间戳记是-1446960600,我想将此时间戳转换为America / Los Angeles吗?
You can convert a unix timestamp integer into a Javascript date like this 您可以像这样将Unix时间戳整数转换为Javascript日期
var date = new Date(1446960600 * 1000);
Unix timestamps are in the UTC timezone, but there isn't an easy way to do timezone changes in Javascript. Unix时间戳位于UTC时区,但是没有一种简单的方法可以在Javascript中进行时区更改。
I recommend you use a time library like momentjs . 我建议您使用诸如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 https://stackoverflow.com/a/18612568/1031569
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. 您可以使用DateTime的
getTimezoneOffset()
方法来计算时区变化,但是要理解,这不会考虑夏令时或leap年变化。
Use momentjs. 使用momentjs。 You can choose your format and you just set the country you need
您可以选择格式,然后设置所需的国家/地区
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.