0

I got this code from a book and I don't understand how this statement "mm=months[mm]" convert to month name string:

function init(){
    var panel = document.getElementById("panel");

    var days=["sun","mon","tue","wed","thur","fri","sat"];
    var months=["jan","feb","mar","apr","may","jun",
    "jul","aug","sep","oct","nov","dec"];

    var now = new Date();

    var yy = now.getFullYear();
    var mm = now.getMonth();
    var dd = now.getDate();
    var dy = now.getDay();

    mm=months[mm];  //convert to month name string
    dy=days[dy];  //convert to month name string

    var str = dy+","+mm","+dd+","+yy;
    panel.innerHTML+="us date string: "+str;
    str = dy+","+dd+" "+mm+","+yy;
    panel.innerHTML+="<br>uk date string: "+str;
}
window.onload=init();

My question is this what exactly does the mm=months[mm] and dy=days[dy] (convert to month or day name string) do. I don't understand how this statement "mm=months[mm]" convert to month name string, when months is an array. Is this just some build in function of an array?

2
  • 4
    It doesn't convert. It takes an index into an array and returns the corresponding value. Re-using the same variables is confusing. days[0] -> "sun", e.g. Commented Jul 7, 2012 at 20:55
  • from the book I think online free tutorials are very good. But it's just my thought. Commented Jul 7, 2012 at 21:19

5 Answers 5

2

mm is a number at the beginning of the statement mm=months[mm], so therefore it returns the mmth value in the months array which is a string. In JavaScript variables are not strictly typed so it then inserts this string into the variable mm

1
  • 1
    got it now, so basically mm is like 11 which if you put it in the array index would give you december Commented Jul 7, 2012 at 21:00
0

mm is a number from 0-11 that represents the month specific to the date object you retrieved it from. months is an array containing the names of all the months. If you say mm = months[mm], you're saying that mm equals the string at position mm in the months array. It's confusing, because they're using the same variable name. It might have been better to do something like this:

var mm = now.getMonth();
alert(mm); // will alert 6
var monthName = months[mm]; // will alert July, which is position 7 in the months array

The reason mm is six is because the months for some reason are 0-indexed, meaning they start at 0 and go to 11; January would be 0, and December 11.

0

days is an array. Given index dy=0, days[dy] returns "sun", which is the 0th value in the array. Similarly, given index dy=1, days[dy] returns "mon", which is the 1st value in the array. And so on.

Same concept for months.

But the correct answer should be "use some built-in function in Javascript or jQuery". See this post for more details. Rolling your own datetime conversion functions is almost certainly going to come back and bite you in the long run.

0

Assume the date is 08 and month is july .

var yy = now.getFullYear();   
var mm = now.getMonth();    // month is 7 now
var dd = now.getDate();     // dd is 8 now
var dy = now.getDay();

Now,

mm=months[mm];

means,

mm = months[7];

// in months array, at seventh index we have july,

so mm is july.

No its not converting but using value from array based on index.

0

mm=month[mm] take the value at the index mm (returned by now.getMonth()) in the months array and puts it in the mm variable. That id the same thing for dy=days[dy].

I recognize that the code is badly written. mm2=month[mm] and use mm2 after (instead of the "new" mm) should be written instead.

3
  • @Mageek cough bullshit cough (Although I would use something more descriptive than mm2.) Commented Jul 7, 2012 at 21:15
  • @Mageek : I totally agree with you but it is MORE convinient to understand how the code works. Good practices are important too. Commented Jul 8, 2012 at 0:29
  • @air-dex If it were me, I would have written var mm=months[new Date().getMonth()]; directly :D Commented Jul 8, 2012 at 9:30

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.