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当前时间多少天后的日期和对应星期
const getDates = function(days, todate = getCurrentMonthFirst()) { //todate默认参数是当前日期,可以传入对应时间
var dateArry = [];
for (var i = 0; i < days; i++) {
var dateObj = dateLater(todate, i);
dateArry.push(dateObj)
}
return dateArry;
}
注释:days (天数) todate(开始日期,如果不传默认就是当前日期)
网友评论