0

I have a very unusual behaviour while creating Date objects using ES:

var someDate = new Date(2019, 2, 21)

gives me Date 2019-02-20T23:00:00.000Z which is one day prior to the date I have passed to constructor.

But if I create new Date without providing any parameters I'm getting correct value:

let now = new Date()

Then now evaluates to 2019-02-21T15:07:21.606Z (today is Feb. 21, 2019).

Stan Reduta
  • 3,292
  • 5
  • 31
  • 55
  • 5
    Let me guess, you're in the central Eurapean timezone. That's why you get a result that's 1 hour back. – VLAZ Feb 21 '19 at 15:14
  • @VLAZ, it's not 1 hour back, it's 1 DAY back! – Stan Reduta Feb 21 '19 at 15:15
  • `new Date(2019, 2, 21)` actually creates a date in March, not February, as months are zero-indexed in the `Date` constructor. – str Feb 21 '19 at 15:15
  • 3
    @Stan it's `23:00`. That's 1 hour away from `00:00` the 21st. – VLAZ Feb 21 '19 at 15:16
  • @VLAZ but I'm not providing tz at all, I just want this particular date as ... date – Stan Reduta Feb 21 '19 at 15:17
  • 2
    @StanRedoute When you create a date it normally assumes the local timezone. Depending on how you print it, it might be UTC or the local timezone. What exactly are you trying to do? We don't know what you mean by "I just want this particular date as ... date". – str Feb 21 '19 at 15:19
  • @StanRedoute you get the UTC value for the date you provided. In this case it's `00:00` on the 21st in UTC. That then gets transformed into your local timezone, so you get `23:00` on the 20th because it's adjusted by shifting one hour back (compensating for your timezone which is one hour forward). When you invoke `new Date()` with no arguments you'll also see that the time is one hour back from your local PC. Around midnight that would be a whole day difference. For somebody at a +6 zone, the difference would persist until morning. – VLAZ Feb 21 '19 at 15:24
  • @VLAZ in this particular case I need date to be just a date. Timezone unaware, without caring about UTC and all of this, meaning I need an object representing some specific date no matter where on earth it's viewed. – Stan Reduta Feb 21 '19 at 16:12

3 Answers3

3

The Date constructor creates a date in your local timezone. To create a date in UTC do this (note that the month parameter is a 0-based index as well):

var someDate = new Date(Date.UTC(2019,1, 21))

or use an ISO 8601 date string, either with no time component or with a time component and the Z suffix to indicate UTC

var someDate = new Date('2019-02-21')
var someDate = new Date('2019-02-21T00:00Z')
Paul
  • 139,544
  • 27
  • 275
  • 264
1

The difference is caused because new Date(2019, 2, 21) creates the date in your current timezone. But when you do console.log(date) it usually prints the date in UTC (different browsers have different behavior).

So when the dates new Date(2019, 2, 21) and new Date() were converted to UTC both were reduced by the same amount but new Date() also gets the current time so the date didn't change.

console.log(new Date(2019, 2, 21));
console.log(new Date(2019, 2, 21).toString());
console.log(new Date(2019, 2, 21).toUTCString());
console.log(new Date());
console.log(new Date().toString());
console.log(new Date().toUTCString());

To create a date in UTC you should either add a Z at the end when parsing from a string.

You can also get the Unix Time for a date by doing Date.UTC(year, month, date). You can create a date from that by doing new Date(Date.UTC(year, month, date))

nick zoum
  • 7,216
  • 7
  • 36
  • 80
0

The 2 in your var someDate = new Date(2019, 2, 21) is the monthIndex.

The argument monthIndex is 0-based. This means that January = 0 and December = 11.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

webbm
  • 634
  • 8
  • 18
  • The problem is with the day, not the month. – Gabriele Petrioli Feb 21 '19 at 15:17
  • Weird, I think I discovered something myself then, because if I look at the output for `var someDate = new Date(2019, 2, 21)`I get `Thu Mar 21 2019 00:00:00 GMT+0100 (Central European Standard Time)` (Mar being the important bit) – webbm Feb 21 '19 at 15:19
  • The OP may well have a misconception about the month bit, but the question was about the day being 1 day prior to the value they passed. And that has to do with the local timezone. – Gabriele Petrioli Feb 21 '19 at 15:23