首页 > 其他分享 >js 获取上一周 下一周 月 年

js 获取上一周 下一周 月 年

时间:2024-10-31 13:24:44浏览次数:3  
标签:date 一周 formatDateTime js 获取 Date new type const

// 获取当前周日期 const getCurrentWeekDates = (type) => {   let currentDate = null   if (type == 'before') {     currentDate = new Date(new Date(getShowDateStartOld.value).getTime() - 7 * 24 * 3600 * 1000)   } else if (type == 'after') {     currentDate = new Date(new Date(getShowDateEndOld.value).getTime() + 1 * 24 * 3600 * 1000)   } else {     currentDate = new Date()   }   const currentDayOfWeek = currentDate.getDay() // 0 (Sunday) through 6 (Saturday)   const startDate = new Date(currentDate)   const endDate = new Date(currentDate)   // 计算起始日期(周一)   startDate.setDate(startDate.getDate() - currentDayOfWeek + 1)   // 计算结束日期(周日)   endDate.setDate(endDate.getDate() - currentDayOfWeek + 7)   // 将日期转换为 YYYY-MM-DD 格式   const start = formatDateTime(startDate, 'date')   const end = formatDateTime(endDate, 'date')   return { start, end } } // 获取月份 const geMonthDates = (type) => {   var date = ''   if (type == 'before') {     date = new Date(new Date(getShowDateStartOld.value).getTime() - 1 * 24 * 3600 * 1000)   } else if (type == 'after') {     date = new Date(new Date(getShowDateEndOld.value).getTime() + 1 * 24 * 3600 * 1000)   } else {     date = new Date()   }   let start = formatDateTime(new Date(date.getFullYear(), date.getMonth(), 1), 'date')   let end = formatDateTime(new Date(date.getFullYear(), date.getMonth() + 1, 0), 'date')   return { start, end } } // 获取年 const geYearDates = (type) => {   var date = ''   if (type == 'before') {     date = new Date(new Date(getShowDateStartOld.value).getTime() - 1 * 24 * 3600 * 1000)   } else if (type == 'after') {     date = new Date(new Date(getShowDateEndOld.value).getTime() + 1 * 24 * 3600 * 1000)   } else {     date = new Date()   }   let yearOld = formatDateTime(new Date(date), 'year')   let start = formatDateTime(new Date(yearOld + '-1-1'), 'date')   let end = formatDateTime(new Date(yearOld + '-12-31'), 'date')   return { start, end } }

标签:date,一周,formatDateTime,js,获取,Date,new,type,const
From: https://www.cnblogs.com/dreammiao/p/18517521

相关文章