Ad

JavaScript Date Always Returns "2015-11-1" Instead Of Today's Date

- 1 answer

 var year = new Date().getFullYear();
 var month = new Date().getMonth();
 var day = new Date().getDay();
 var currDate = year + "-" + month + "-" + day;

New Date() will return today's date. Above code should return today's date like "2015-12-18", but it returns "2015-11-1". Anyone knows why? Thanks.

Ad

Answer

Months are zero based, so you always have to add 1 to get the correct month.

var month = new Date().getMonth() + 1;

or subtract 1 if setting it

getDay() is just the wrong method, it gets the day of the week, 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on, not the date, that would be

var day = new Date().getDate();
Ad
source: stackoverflow.com
Ad