简体   繁体   中英

Converting unix date-timestamp to time for a particular timezone in javascript

I want to convert unix timestamp to time for a particular timezone.

say for example unix time stamp is - 1446960600 I want to convert this timestamp to America/Los Angeles ?

You can convert a unix timestamp integer into a Javascript date like this

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.

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

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.

Use momentjs. You can choose your format and you just set the country you need

http://momentjs.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM