Reputation: 196
I'm currently working on a function to generate data according to a start and an end date.
While I was testing my functions I've realized some wrong calculated values.
Long story short the getMonth()
method for Date
objects is returning a unexpected value.
var date = new Date();
console.log(date.getMonth());
var date2 = new Date(2014, 1, 1);
console.log(date2);
Can someone explain to me the reason why to start a month at 0, even if it's not possible to create a right Date
object using that index.
Even though date2
my second example is from the official docu of JS and is returning a unexpected value according to the example giving in the "Parameters" section.
Upvotes: 3
Views: 24268
Reputation: 196
As @Airwavezx mentioned the code was correct.
tl;tr I forgot to use timezones and JS is counting months from 0
So a solution to my question could be:
var date = new Date();
console.log(date.getMonth()+1);
var date2 = new Date("2014-01-01T00:00:00Z");
console.log(date2);
Upvotes: 3