Misha Moroshko Misha Moroshko - 1 month ago 16
Javascript Question

How to get the day of the week from the day number in Javascript?

Given

dayNumber
is from
0..6
representing
Monday..Sunday
respectively.

Can the
Date
/
String
objects be used to get the day of the week from
dayNumber
?

Answer

This will give you a day based on the index you pass:

var weekday=new Array(7);
weekday[0]="Monday";
weekday[1]="Tuesday";
weekday[2]="Wednesday";
weekday[3]="Thursday";
weekday[4]="Friday";
weekday[5]="Saturday";
weekday[6]="Sunday";
console.log("Today is " + weekday[3]);

Outputs "Today is Thursday"

You can alse get the current days index from JavaScript with getDay() (however in this method, Sunday is 0, Monday is 1, etc.):

var d=new Date();
console.log(d.getDay());

Outputs 1 when it's Monday.