2

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?

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

3 Answers 3

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.

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.

1

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

5
  • 1
    Just a note, MDN is the preferred resource for documentation about javascript, where everyone can contribute, as opposed to w3fools... ehrm, w3schools.
    – Patrick
    Commented Jul 8, 2014 at 10:59
  • @Patrick haha.. I think everybody contributes to MDN but it is W3C which maintains these standards.
    – gprathour
    Commented Jul 8, 2014 at 11:08
  • 3
    W3C, yes, not w3schools. They are not affiliated (en.wikipedia.org/wiki/W3Schools). Also; w3fools.com
    – Patrick
    Commented Jul 8, 2014 at 11:10
  • @Patrick Thanks man ! I used to think W3Schools belongs to W3C. They are not even affiliated to them. Wow..!!
    – gprathour
    Commented Jul 8, 2014 at 11:17
  • Also, the W3C does not maintain ECMAScript / JavaScript. It only describes interfaces for browser scripting languages such as JavaScript or VBScript.
    – Kijewski
    Commented Jul 8, 2014 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.