javascript - Why there is need to add 1 to the month for this date conversion? - Stack Overflow

I have this date variable in javascript $scope.dt and the contents is Tue Jul 08 2014 00:00:00 GMT+0800

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 that is 2014-7-8 (YYYY-MM-DD).

Below is the function I wrote;

function convertDate_YYYYMMDD(d)
{
    var curr_date = d.getDate();
    var curr_month = d.getMonth()+1; //why need to add one?
    var curr_year = d.getFullYear();
    return (curr_year + "-" + curr_month + "-" + curr_date );
}

It works fine. What I don't understand is why do I need to add 1 to get the correct curr_month? If I do not do this, the month will always be off by one. The code works but I don't know why it works.

Can someone advise?

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 that is 2014-7-8 (YYYY-MM-DD).

Below is the function I wrote;

function convertDate_YYYYMMDD(d)
{
    var curr_date = d.getDate();
    var curr_month = d.getMonth()+1; //why need to add one?
    var curr_year = d.getFullYear();
    return (curr_year + "-" + curr_month + "-" + curr_date );
}

It works fine. What I don't understand is why do I need to add 1 to get the correct curr_month? If I do not do this, the month will always be off by one. The code works but I don't know why it works.

Can someone advise?

Share Improve this question edited Jul 8, 2014 at 10:53 gprathour 15.3k5 gold badges71 silver badges92 bronze badges asked Jul 8, 2014 at 10:52 guagay_wkguagay_wk 28.1k64 gold badges200 silver badges309 bronze badges 5
  • 4 In JavaScript, the index of the Month begins from 0 – Paul S. Commented Jul 8, 2014 at 10:53
  • 1 Ok. It was a trivial question but at least I wrote the code myself. Why the negative point? I didn't expect month to start from zero but not the days. – guagay_wk Commented Jul 8, 2014 at 11:00
  • 2 The negative point might be because a simple search query turns this up in less time than it takes for you to type this question. – Patrick Commented Jul 8, 2014 at 11:14
  • @Patrick: You're right. Upvoted. It wasn't that I was too lazy to search. I just didn't expect that month starts from zero when days start from one. The language should be consistent. Feel like a damn fool now. What a trivial question. – guagay_wk Commented Jul 8, 2014 at 12:27
  • @user3293156: Don't feel bad. The next time someone searches for this they might find your question, and that may well help them understand why this happens. Consider how many you will have helped in a years time just by asking this. ;-) – Patrick Commented Jul 8, 2014 at 12:30
Add a ment  | 

3 Answers 3

Reset to default 5

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./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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745492692a4630055.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信