1. 程式人生 > 實用技巧 >如何獲得未來n天的日期及是對應的星期幾

如何獲得未來n天的日期及是對應的星期幾

/**
 * 獲取時間
 * day_count 是多少天
 * 
 */
function getDays(day_count) {
    var date = new Date();
    var dates = [];
    var week = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
    for (var i = 0; i <= day_count; i++) {
        var d = null;
        ""
        var date1 = date.getDate();
        var month = date.getMonth()+1;
        if(month < 10){
            month = "0" + month;
        }
        if(date1 < 10){
            date1 = "0" + date1;
        }
        d = date.getFullYear() + "-" + month + "-" + date1;
        var weeks = "";
        var day = new Date(d);
        var z = day.getDay();
        weeks = week[z];
        dates.push({
            "date": d,
            "week": weeks
        });
        date.setDate(date.getDate() + 1);
    }
    return dates;
};

  

//呼叫函式,以未來7天為例
var dataList = getDays(7);

 參考:https://www.cnblogs.com/huaji666/p/6814354.html