const getCurrentMonthFirst = function() {
var date = new Date();
var todate = date.getFullYear() + "-" + ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1) + "-" + (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
return todate;
}
/**
- 傳入時間后幾天
- param:傳入時間:dates:"2018-04-02",later:往后多少天
*/
const dateLater = function(dates, later) {
let dateObj = {};
let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
let date = new Date(dates);
date.setDate(date.getDate() + later);
let day = date.getDay();
dateObj.year = date.getFullYear();
dateObj.month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
dateObj.day = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
dateObj.week = show_day[day];
return dateObj;
}
//獲取d當(dāng)前時間多少天后的日期和對應(yīng)星期
const getDates = function(days, todate = getCurrentMonthFirst()) { //todate默認(rèn)參數(shù)是當(dāng)前日期声怔,可以傳入對應(yīng)時間
var dateArry = [];
for (var i = 0; i < days; i++) {
var dateObj = dateLater(todate, i);
dateArry.push(dateObj)
}
return dateArry;
}
注釋:days (天數(shù)) todate(開始日期,如果不傳默認(rèn)就是當(dāng)前日期)