Why there is need to add 1 to the month for this date conversion?

放肆的年华 提交于 2019-12-02 10:52:32
kay

That's legacy of C. The month in timestamps are zero-based.

Compare Date.getMonth():

The value returned by getMonth is an integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.

struct tm:

int tm_mon month of year [0,11]

And why the months start with zero in many programming languages is explained in here: Zero-based month numbering. Paraphrased: Using January == 0 was useful in ancient times, and now we re stuck with it.

http://www.w3schools.com/jsref/jsref_getmonth.asp

The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.

Note: January is 0, February is 1, and so on.

The month range is 0-11. i.e. For January it will be 0 and for December it will return you 11. Therefore we need to add 1 to it.

Check this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!