getMonth in javascript gives last month

2019-01-04 01:55发布

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?

2条回答
该账号已被封号
2楼-- · 2019-01-04 02:26

Because getmonth() start from 0. You may want to have d1.getMonth() + 1 to achieve what you want.

查看更多
神经病院院长
3楼-- · 2019-01-04 02:33

getMonth() function is zero indexed based. You need to do d1.getMonth() + 1

Recently I used Moment.js library and never looked back. Try it!

查看更多
登录 后发表回答