1. Home
  2. Programming Tools
  3. Date function in JavaScript creating Wrong month? [Solved]
  1. Home
  2. Web designing
  3. Date function in JavaScript creating Wrong month? [Solved]

Date function in JavaScript creating Wrong month? [Solved]

Question: While using Date function in JavaScript, the month created is wrong. For example, for displaying the month of November, I pass ’11’ as an argument to the date function. But the result is December. Below is the function reference and its possible arguments.

new Date(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]]);

E.g:, var sampleDate = new Date(2016, 11, 15, 0, 0, 0,0);

Sample output : Tue Dec 15 2016 00:00:00

Can you please explain this behavior?

Solution:

date function

The behavior is right. The Date function in JavaScript will start with zero (0). Hence, the 10th month is November and 11th month is December. The month field is an integer value, starting with 0 for January and 11 for December. That’s weird isn’t? But that’s how it is.

Reference: JavaScript reference.

Updated on September 1, 2017

Was this article helpful?

Related Articles

Add A Comment