dotmax
dotmax

Reputation: 21

JavaScript - convert to string using array index

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?

Upvotes: 0

Views: 306

Answers (5)

air-dex
air-dex

Reputation: 4180

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.

Upvotes: 0

Jashwant
Jashwant

Reputation: 29005

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.

Upvotes: 0

James Lim
James Lim

Reputation: 13054

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.

Upvotes: 0

Elliot Bonneville
Elliot Bonneville

Reputation: 53311

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.

Upvotes: 0

jaypeagi
jaypeagi

Reputation: 3141

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

Upvotes: 2

Related Questions