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

Ashis Jena
  • 450
  • 1
  • 8
  • 17
  • What do you get if you do `console.log(dateInCST);`? Do you get the same date as you send? – Anthony C Mar 06 '17 at 17:02
  • 1
    It depends on the server timezone, if it is set to UTC it will always shows in date and time in UTC timezone, similarly browser also behaves in same way(gets the timezone from OS) – Pavan Kumar Jorrigala Mar 06 '17 at 19:48
  • @PavanKumarJorrigala I'm running this in local system. The timezone of my system is IST i.e +5:30. I've given more detail in the question. – Ashis Jena Mar 07 '17 at 08:32
  • 1
    You might want to look at [`Date.UTC()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC). – robertklep Mar 07 '17 at 08:54
  • One can't create a variable like `dateInCST` using the `Date` object alone. The `Date` object is always UTC internally, and uses the local time zone for conversions... You can do this with [moment.js](http://momentjs.com), but not a raw `Date`. – Matt Johnson-Pint Mar 07 '17 at 19:51
  • @MattJohnson The dateInCST is nothing but deducting "-06:00" or "-05:00" hour from the dateInUTC. Though the timezone is will not be in CST, but the dateInCST represent the CST hour and date. – Ashis Jena Mar 08 '17 at 12:50
  • That technique is called "epoch shifting". It only works reliably if you are very strict about using only the UTC-based functions. If you try to shift local time, errors will occur around any DST transitions of the local time zone. Be careful - thar be dragons here. :) – Matt Johnson-Pint Mar 08 '17 at 19:33

5 Answers5

5

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
Pavan Kumar Jorrigala
  • 3,085
  • 16
  • 27
5

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();

3

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.

Robert Rossmann
  • 11,931
  • 4
  • 42
  • 73
1

You can append the string "UTC" at the end of date string passed in new Date() eg:

var date= new Date("2019-2-21 UTC")

now var date will be in local time

var d = new Date();
var date = new Date(d + 'UTC');
console.log(d)// date in UTC 
console.log(date)// date in localtime

`

  • 1
    Returns an invalid date in Safari at least. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Nov 21 '19 at 07:21
1
const test = new Date();
const test1 = new Date(test.setUTCHours(0,0,0,0));
barbsan
  • 3,418
  • 11
  • 21
  • 28