1.开发环境 vue
2.电脑系统 windows10专业版
3.在开发的过程中,咱们常常会须要获取到以后日期的本周第一天和最初一天以及本月的第一天和最初一天,上面我来分享一下如何实现。
4.废话不多说:
//获取以后日期 本周 第一天和 本周最初一天
function getweekday(getweekdays) {
var date = new Date(getweekdays);
// 本周一的日期
date.setDate(date.getDate() - date.getDay() + 1);
var begin = date.getFullYear() + "-" + ((date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1)) + "-" + (date.getDate() < 10 ? "0" + date.getDate() : date.getDate());
// 本周日的日期
date.setDate(date.getDate() + 6);
var end = date.getFullYear() + "-" + ((date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1)) + "-" + (date.getDate() < 10 ? "0" + date.getDate() : date.getDate());
let timeInfo = {
begin: begin,
end: end
}
return timeInfo
}
//留神:输入格局为: 2021-03-29
//在这里通过三木运算符解决如果月份和日期小于10,就主动步0.
5.以后日期的本月第一天和最初一天:
function getCurDay(getCurDays) {
let nowdays = new Date(getCurDays);
let year = nowdays.getFullYear();
let month = nowdays.getMonth() + 1;
month = month > 9 ? month : "0" + month;
let firstDayOfCurMonth = `${year}-${month}-01`;
let lastDay = new Date(year, month, 0);
let lastDayOfCurMonth = `${year}-${month}-${lastDay.getDate()}`;
let timeInfo = {
firstDayOfCurMonth: firstDayOfCurMonth,
lastDayOfCurMonth: lastDayOfCurMonth
}
return timeInfo;
}
6.本期的分享到了这里就完结啦,心愿对你有所帮忙。
发表回复