js判断某个具体日期为周几

代码2019-05-22156 人已阅来源:网络

如何根据某一个指定的日期,来判断这一天具体是周几?

一般一星期顺序如下:

"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"

具体解决方法如下

function getMyDay(date) {
    var week;
    if (date.getDay() == 0) week = "周日"
    if (date.getDay() == 1) week = "周一"
    if (date.getDay() == 2) week = "周二"
    if (date.getDay() == 3) week = "周三"
    if (date.getDay() == 4) week = "周四"
    if (date.getDay() == 5) week = "周五"
    if (date.getDay() == 6) week = "周六"
    return week;
}
var tonjayWeek = getMyDay(new Date("2019-5-22"));
console.log(tonjayWeek)

可以复制代码在Chrome开发者工具console里直接粘贴回车,即见效果。