任意给定时间(年月日)计算星期几

示例:

 <input type="number" name="year" id="year" style="width: 100px" ><input type="number" name="month" id="month" style="width: 100px" ><input type="number" name="day" id="day" style="width: 100px" >

结果:

 var y = document.getElementById('year').value;
        var m = document.getElementById('month').value;
        var d = document.getElementById('day').value;
        y = parseInt(y);
        m = parseInt(m);
        d = parseInt(d);
        if (m == 1) { m = 13; y--; }
        if (m == 2) { m = 14; y--; }
        var week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7 + 1;
        week = Math.floor(week);

        var weekstr = "";
        switch (week)
        {
            case 1: weekstr = "星期一"; break;
            case 2: weekstr = "星期二"; break;
            case 3: weekstr = "星期三"; break;
            case 4: weekstr = "星期四"; break;
            case 5: weekstr = "星期五"; break;
            case 6: weekstr = "星期六"; break;
            case 7: weekstr = "星期日"; break;
        }
        console.log(weekstr);

 

posted @ 2018-06-19 15:20  万里冰封  阅读(1285)  评论(0编辑  收藏  举报