Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new Date(year, month, date) in node js is automatically getting converted to UTC timezone

    var dateInCST; //Getting CST date as input.

    /*Triming the time part and retaining only the date.*/
    var onlyDateInCST = new Date(dateInCST.getUTCFullYear(), dateInCST.getUTCMonth(), dateInCST.getUTCDate()); 

    console.log(onlyDateInCST);

I'm in +5:30 i.e IST time zone.

During the date creation by providing year, month and date, the node js is treating it as IST and deducting -5:30 automatically.

Node js is converting the date to UTC automatically by considering the date to be at server timezone. But in browser I'm getting proper CST date without time.

Example :

var today = new Date(2017, 2, 7);
console.log(today);

The date should be 2017-03-07T00:00:00.000Z. But node js deducts the server time zone difference between UTC i.e +5:30 from this date and the date object becomes 2017-03-06T18:30:00.000Z

Why the above code is behaving different in Node js from browser. Any workaround for this?

Edit :

var date = new Date();
function createDateAsUTC(date) {
    return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds()));
}
console.log(date);
console.log(createDateAsUTC(date));

NodeJs output :

2017-03-08T12:28:16.381Z

2017-03-08T17:58:16.000Z

Browser Output :

Wed Mar 08 2017 17:58:17 GMT+0530 (India Standard Time)

Wed Mar 08 2017 23:28:17 GMT+0530 (India Standard Time)

There is difference between Node js behaviour and browser. The server(local) time was 17:58.

What's the difference between new Date(?,?,?,?,?,?) and new Date(Date.UTC(?,?,?,?,?,?)) ?

like image 739
Ashis Jena Avatar asked Mar 06 '17 16:03

Ashis Jena


Video Answer


3 Answers

Nodejs is representing in ISO 8601 format

Z is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "0930Z"

In your case

var today = new Date(2017, 2, 7);

2017-03-06T18:30:00.000Z Its valid, because it is representing in UTC.

May be default date format in nodejs is yyyy-mm-ddThh:mm:ss.fffZ, if you change it to yyyy-mm-ddThh:mm:ss.fffZ+|-hh:mm it will show as 2017-03-07T00:00:00.000+05:30


Update

I ran the same date and timezone as yours, On my machine it is not showing ISO 8601 format, it only showed with toISOString() method

var today = new Date(2017, 2, 7);
console.log(today); --> Tue Mar 07 2017 00:00:00 GMT+0530 (India Standard Time)
console.log(today.toISOString()); --> 2017-03-06T18:30:00.000Z

to replicate I added moment.js

var today = new Date(2017, 2, 7);
console.log(moment(today).format()); --------> 2017-03-07T00:00:00+05:30
console.log(moment(today).utc().format()); --> 2017-03-06T18:30:00Z
like image 125
Pavan Kumar Jorrigala Avatar answered Oct 24 '22 21:10

Pavan Kumar Jorrigala


If yo want exact same as in Browser Just convert it to string by calling toString method like following. Then you will get correct date as you expected.

(new Date()).toString();

like image 31
Rajasekhar Kunati Avatar answered Oct 24 '22 22:10

Rajasekhar Kunati


When you create a Date object (regardless of whether in browser or in Node.js), the instance is created with the date being interpreted as local to the current timezone. See the second note in MDN docs.

If you would like to create the date in UTC right away, you must use

new Date(Date.UTC(...))

Date.UTC() has the same input semantics as new Date().

Another thing to note here is that printing the date instance to the console will cause the date to be represented as an ISO timestamp which is always in UTC. You may wish to inspect the date object by printing date.toString() or date.toLocaleString() which will show the date in the current timezone.

like image 2
Robert Rossmann Avatar answered Oct 24 '22 20:10

Robert Rossmann