Skip to content Skip to sidebar Skip to footer

Why There Is Need To Add 1 To The Month For This Date Conversion?

I have this date variable in javascript $scope.dt and the contents is Tue Jul 08 2014 00:00:00 GMT+0800 (Malay Peninsula Standard Time). I want to convert it to return a string tha

Solution 1:

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.


Solution 2:

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.


Solution 3:

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


Post a Comment for "Why There Is Need To Add 1 To The Month For This Date Conversion?"