Why is Javascript returning the wrong UTC month value?

Well ... for starters ... let me say that I've done this a thousand times. I am just trying to print utc in JavaScript.

But ... the value I got is wrong.

Instead of (9) September, JavaScript returns (8) for August. From today - September 2, 2012.

UTC time will look something like this: 2014-09-02 07:00:02

.

Instead, I get 2014-08-02 07:00:02

.

I turned on the violin. Please take a look at her.

FIDDLE

+3


source to share


2 answers


Months in JavaScript are returned as a 0-based value.

0 January
1 Feburary
...
8 September
9 November
...

      



RTFM:

The value returned by getUTCMonth is an integer from 0 to 11 corresponding to the month. 0 for January 1 for February 2 for March, etc.

+6


source


The getMonth () method returns the month (0 to 11) for the specified date according to UTC.

You can try using an array.

var d = new Date()
var month = new Array(12);
month[0] = "January";
month[1] = "February";

      



...

var n = month [d.getUTCMonth ()];

+2


source







All Articles