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(?,?,?,?,?,?)) ?