Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS timezone conversion

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.

like image 392
ndg Avatar asked Jul 12 '26 04:07

ndg


2 Answers

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
like image 129
nemisj Avatar answered Jul 14 '26 18:07

nemisj


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

like image 26
Ali Salehi Avatar answered Jul 14 '26 18:07

Ali Salehi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!