计算周一的日期
getMondayofWeek (date) {
// date参数为Date格式
const day = date.getDay() || 7;
return new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1 - day);
}
计算周日的日期
getSundayofWeek (date) {
// date参数为Date格式
const day = 7 - (date.getDay() || 7);
return new Date(date.getFullYear(), date.getMonth(), date.getDate() + day);
},
获取某年某月的最后一天
getMonthFinalDay (year, month) {
// year, month是字符串格式
let day = '';
if (year == null || year === undefined || year === '') {
year = new Date().getFullYear();
}
if (month == null || month === undefined || month === '') {
month = new Date().getMonth() + 1;
}
day = new Date(new Date(year, month).setDate(0)).getDate();
return year + '-' + month + '-' + day
},
计算某日是第几周
getMonthWeek (a, b, c) {
// 参数是number格式,例如:new Date().getYullYear()返回数字格式,abc对应年月日
const date = new Date(a, parseInt(b) - 1, c)
const day = date.getDay() || 7
const d = date.getDate()
return Math.ceil((d + 6 - day) / 7)
},
获取某月的上个月
getPreMonth (yearmonth) {
// yearmonth是'yyyy-mm'格式
const arr = yearmonth.split('-')
const year = arr[0]
const month = arr[1]
let year2 = year;
let month2 = parseInt(month) - 1;
if (month2 === 0) {
// 1月的上一月是前一年的12月
year2 = parseInt(year2) - 1;
month2 = 12;
}
if (month2 < 10) {
// 10月之前都需要补0
month2 = '0' + month2;
}
return year2 + '-' + month2;
},
时间日期Date格式转换为'yyyy-mm-dd'格式
dateformat (date) {
// date为Date格式
const year = date.getFullYear()
let month = date.getMonth() + 1
let day = date.getDate()
month = (month > 9) ? month : ('0' + month)
day = (day < 10) ? ('0' + day) : day
return year + '-' + month + '-' + day
},
时间戳返回 yyyy-MM-dd 格式字符串
dateToYYYYMMDD (date) {
// date为时间戳格式
const time = new Date(date);
const y = time.getFullYear();
let m = (time.getMonth() + 1);
m = m > 9 ? m : '0' + m;
let d = time.getDate();
d = d > 9 ? d : '0' + d;
return y + '-' + m + '-' + d
},