最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS timezone conversion - Stack Overflow

programmeradmin0浏览0评论

In Javascript, I have a timestamp which I'm handling like so:

var origUnixTimestamp = (date * 1000);

Along with this timestamp I have a UTC offset (-5, although this is variable). I'm looking to convert origUnixTimestamp to the users UTC offset, using Date's getTimezoneOffset() method.

I'm just wondering how I take into account the original timestamps UTC offset (-5, for example) and convert it to the users current UTC offset. I imagine this is fairly simple, but it's warping my brain at the moment.

In Javascript, I have a timestamp which I'm handling like so:

var origUnixTimestamp = (date * 1000);

Along with this timestamp I have a UTC offset (-5, although this is variable). I'm looking to convert origUnixTimestamp to the users UTC offset, using Date's getTimezoneOffset() method.

I'm just wondering how I take into account the original timestamps UTC offset (-5, for example) and convert it to the users current UTC offset. I imagine this is fairly simple, but it's warping my brain at the moment.

Share Improve this question edited Feb 14, 2011 at 12:38 Justin Ethier 134k52 gold badges232 silver badges287 bronze badges asked Dec 15, 2009 at 19:48 ndgndg 311 silver badge2 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Javascript doing it for you. All the dates stored in the Date object are already converted to the correct timezone (just pass your epoch to the constructor). The same Date object has ability to work with epoch date and UTC.

var some_date = new Date(epoch);
var time = some_date.getDay(); // will be different in different zones
some_date.setDay(22); // to set day
var origUnixTimestamp = some_date.getTime(); //returns you epoch

How about this: http://github./mde/timezone-js

This link has instructions for converting from local time:

// create Date object for current location
d = new Date();

// convert to msec
// add local time zone offset
// get UTC time in msec
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
发布评论

评论列表(0)

  1. 暂无评论