I am using a datepicker which gives a date in the format Sun Jul 7 00:00:00 EDT 2013. Even though the month says July, if I do a getMonth, it gives me the previous month.
var d1 = new Date("Sun Jul 7 00:00:00 EDT 2013");
d1.getMonth());//gives 6 instead of 7
What am I doing wrong?
Because getmonth() start from 0. You may want to have
d1.getMonth() + 1
to achieve what you want.getMonth()
function is zero indexed based. You need to dod1.getMonth() + 1
Recently I used Moment.js library and never looked back. Try it!