clint
clint

Reputation: 1956

Javascript Date object using variables

My js code is as follows:-

   var earlierdate=new Date(2012,09,22);
   alert(earlierdate.getDay());

   var date2 = new Date('2012,09,22');
   alert(date2.getDay());

The problem is first alert gives me 1 0r Monday(which is incorrect) and second one gives me 6 or sat which is correct. So date given in quotes is giving correct result. Now if want to use variables instead of hard-coded values like

var date1 = new Date(a,b,c);
alert(date1.getDay());

What should be the syntax. I have tried a lot of variations but failed.

Thanks in advance.

Upvotes: 0

Views: 56

Answers (2)

Keethanjan
Keethanjan

Reputation: 363

 //Option 1
 var myDate=new Date();
 myDate.setFullYear(2010,0,14);

//Option 2 (Neater)
var myDate=new Date(2010,0,14);

This will set the time to 14th January 2010.

Upvotes: 1

xdazz
xdazz

Reputation: 160923

The month parameter of Date is 0-indexed.

month

Integer value representing the month, beginning with 0 for January to 11 for December.

So if you mean September, that should be var earlierdate=new Date(2012, 8, 22);

Upvotes: 1

Related Questions