Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use timezone offset in Nodejs?

I need the next flow:

var a = new Date(1337324400000, 'Europe/Amsterdam'); //+2h console.log(a); // for example 12:00 Mon ... a.setTimeZone('Europe/Kiev'); //+3h console.log(a); // 13:00 Mon ... 

Is there such possibility in nodejs utils api ?

like image 781
Oleg Dats Avatar asked May 16 '12 09:05

Oleg Dats


People also ask

How do you use timezone offset?

The zone offset can be Z for UTC or it can be a value "+" or "-" from UTC. For example, the value 08:00-08:00 represents 8:00 AM in a time zone 8 hours behind UTC, which is the equivalent of 16:00Z (8:00 plus eight hours). The value 08:00+08:00 represents the opposite increment, or midnight (08:00 minus eight hours).

How do I get timezone from offset?

The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.

How does node js handle timezone?

thejh is right, you cannot change the timezone. Use a JS time library (like moment. js) and add / subtract hours instead. The easiest and the correct way to do this is to simply change your system's time zone.

How do you use UTC time in node JS?

To get a UTC Date object in Node. js and JavaScript, we can call the date's toUTCString method. to create a new Date instance and call toUTCString to return the UTC date string version of the current date time.


Video Answer


1 Answers

You can use node-time, as follows:

var time = require('time');  var a = new time.Date(1337324400000);  a.setTimezone('Europe/Amsterdam'); console.log(a.toString()); // Fri May 18 2012 09:00:00 GMT+0200 (CEST) a.setTimezone('Europe/Kiev'); console.log(a.toString()); // Fri May 18 2012 10:00:00 GMT+0300 (EEST) 
like image 194
Laurent Couvidou Avatar answered Oct 09 '22 02:10

Laurent Couvidou