-2

Anyone can tell me why if I type this:

var datedate1 = new Date(2017, 05, 17);
console.log(datedate1);

The console shows: 2017-06-16T22:00:00.000Z?

Thank you.

UmarZaii
  • 1,355
  • 1
  • 17
  • 26
  • Because you ignored two well known facts; first that the month is counted from 0 to 11, and secondly that time zones exist. – CBroe Jul 18 '17 at 09:33
  • See [*unexpected javascript date behavior*](https://stackoverflow.com/questions/10706272/unexpected-javascript-date-behavior) or [*setDate() unexpected behaviour wrong month*](https://stackoverflow.com/questions/18146897/javascript-setdate-unexpected-behaviour-wrong-month), there are a lot of duplicates. – RobG Jul 18 '17 at 11:39

1 Answers1

0

There are 2 issues here:

  1. Month is 0-indexed, to get 5 you need to pass through 04 for the month
  2. The date used uses UTC time prior to localising to your timezone(GMT-2)? so you will need to account for your timezone and pass through those arguements.

Another option is to look at using a framework like moment https://www.npmjs.com/package/moment

Greg Rebisz
  • 156
  • 7
  • Re "*The date used uses UTC time prior to localising to your timezone*" The host time zone offset is used to generate the Date in the first place, the issue here is that the output timezone is UTC+00:00. *toString* will typically use local. I wouldn't call moment.js a "framework", it's a library (i.e. a bundle of useful functions). – RobG Jul 18 '17 at 11:36