【记录】js|【记录】js 获取周、月、年的开始及结束时间

/* 时间加一天 */ const getTimes = (time)=>{ time = new Date(time.getTime() + 3600 * 1000 * 24 * 1); const year = time.getFullYear(); const month = (time.getMonth() + 1).toString().padStart(2, "0"); const day = time.getDate().toString().padStart(2, "0"); return `${year}-${month}-${day}`; }/* 时间格式处理 */ const timeFormat = (date,status) => { const y = date.getFullYear(); //年 const m = (date.getMonth() + 1).toString().padStart(2, '0'); //月 const d = date.getDate().toString().padStart(2, '0'); //日 if(status) return `${y}-${m}-${d} 00:00:00`; return `${y}-${m}-${d} 23:59:59`; }/* ------------------------------------------------------ */ /* 本周星期一 */ const getFirstDayOfWeek = (date,status=true) => { const weekday = date.getDay() || 7; date.setDate(date.getDate() - weekday + 1); if(status) return timeFormat(date,status); return getWeekLast(date); }/* 本周最后一天 */ const getWeekLast = (data)=>{ const nowTime = data.getTime() ; const day = data.getDay(); const oneDayTime = 24*60*60*1000 ; const SundayTime =nowTime + (7-day)*oneDayTime; return timeFormat(new Date(SundayTime)); }/* ------------------------------------------------------ */ /* 本月第一天 */ const getFirstDayOfMonth = (date,status=true)=>{ date.setDate(1); if(status) return timeFormat(date,status,'月'); return CurrentMonthLast(date); } /* 本月最后一天 */ const CurrentMonthLast = (date)=>{ let currentMonth=date.getMonth(); let nextMonth=++currentMonth; let nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1); let oneDay=1000*60*60*24; return timeFormat(new Date(nextMonthFirstDay-oneDay)); }/* ------------------------------------------------------ */ /* 本年年初 */ const getFirstDayOfYear = (date,status=true)=>{ date.setDate(1); date.setMonth(0); if(status) return timeFormat(date,status,'年'); return getEndYear(date); } /* 本年年尾 */ const getEndYear = (date)=>{ date.setFullYear(date.getFullYear() + 1); date.setMonth(0); date.setDate(0); return timeFormat(date); }

    推荐阅读