js 获取当前日期任意天数的日期 任意月份的起止日期
作者:互联网
// 根据周一的日期获取周日的日期
let time = new Date();
let y = time.getFullYear();
let m = time.getMonth() + 1;
let d = time.getDate();
let date2 = y + '-' + m + '-' + d;
let weekEnd;
getWeeklast(6,date2);
function getWeeklast(num, weekStart) {
let newDate = time;
newDate.setDate(newDate.getDate() + num);
let y = newDate.getFullYear();
let m = newDate.getMonth() + 1;
let d = newDate.getDate();
let date1 = y + '-' + m + '-' + d;
weekEnd = date1;
}
console.log('当前时间' + date2);
console.log('一周后时间'+ weekEnd);
// 获取一个月的开始时间和结束时间 let monthStart; let monthEnd; getMonthStartEndDay(y,m); function getMonthStartEndDay(year,month){ let startDay = new Date(year,month,1); monthStart = y + '-' + m + '-' + startDay.getDate(); let endDay = new Date(startDay.getTime() - 1000 * 60 * 60 * 24).getDate(); monthEnd = y + '-' + m + '-' + endDay; } console.log('当前月初的时间' + monthStart); console.log('当前月末的时间' + monthEnd);
// 获取一个月的开始时间和结束时间 let monthStart; let monthEnd; getMonthStartEndDay(y,m); function getMonthStartEndDay(year,month){ let startDay = new Date(year,month,1); monthStart = y + '-' + m + '-' + startDay.getDate(); let endDay = new Date(startDay.getTime() - 1000 * 60 * 60 * 24).getDate(); monthEnd = y + '-' + m + '-' + endDay; } console.log('当前月初的时间' + monthStart); console.log('当前月末的时间' + monthEnd);
标签:let,newDate,getDate,time,console,log,startDay 来源: https://www.cnblogs.com/zhengao/p/10252026.html