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

后端 未结 8 1123
野的像风
野的像风 2020-12-03 07:05

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

Can the Date /

8条回答
  •  庸人自扰
    2020-12-03 07:34

    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.

提交回复
热议问题