0

Possible Duplicate:
javascript is creating date wrong month

In my code, I create Date by this line:

var date = new Date('2012', '01', '20')

and then using:

alert(date);

I get the result:

enter image description here

I don't really understand why not Jan instead of Feb?

jsFiddle: http://jsfiddle.net/U5m8N/

Community
  • 1
  • 1
cuongle
  • 74,024
  • 28
  • 151
  • 206
  • 5
    Month parameter is 0 based, not 1. January is 0, February is 1. See [Date](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date) – Salman A Feb 04 '13 at 09:08
  • Also, the parameters should be integers, not strings. – JJJ Feb 04 '13 at 09:13

2 Answers2

2

The "Month" part of var date = new Date('2012', '01', '20'); is zero-indexed.

Start counting at 0, and you got your month. (So, January is 0, Feb 1, etc.)

Also, while JavaScript does accept strings as parameters, you should be using integers, as the documentation suggests:

Date Documentation

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
-1

Javascript Date Object's Month is 0 indexed. http://www.w3schools.com/jsref/jsref_obj_date.asp. put 0 instead of 1 for Jan

maimoona
  • 617
  • 8
  • 16