获取指定时间戳 所在周 所在月 所在月和前一个月和后一个月 的时间

//  获取指定时间戳所在一周的时间
  function     formatDate(date) {
      const year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      if (month < 10) {
        month = `0${month}`
      }
      if (day < 10) {
        day = `0${day}`
      }
      return `${year}-${month}-${day}`
    }

   function getMonthdays(timestamp) {
      let MonthDate = new Date(timestamp)
      const year = MonthDate.getFullYear()
      const month =
        MonthDate.getMonth() + 1 < 10
          ? `0${MonthDate.getMonth() + 1}`
          : MonthDate.getMonth() + 1
      let dayArr = []
      let eddDate = new Date(year, MonthDate.getMonth() + 1, 0).getDate()

      let starStamp = new Date(`${year}-${month}-01`).getTime()

      console.log(starStamp, month)

      for (let i = 0; i < eddDate; i++) {
        let val = Number(starStamp) + i * 86400000
        console.log(val)
        dayArr.push(formatDate(new Date(val)))
      }

      console.log(dayArr, eddDate)
    }   
//获取指定时间戳所在月整月的时间
   function getMonthdays(timestamp) {
      let MonthDate = new Date(timestamp)
      const year = MonthDate.getFullYear()
      const month =
        MonthDate.getMonth() + 1 < 10
          ? `0${MonthDate.getMonth() + 1}`
          : MonthDate.getMonth() + 1
      let dayArr = []
      let eddDate = new Date(year, MonthDate.getMonth() + 1, 0).getDate()

      for (let i = 1; i <= eddDate; i++) {
        dayArr.push(`${year}-${month}-${i<10?`0${i}`:i}`)
      }

      console.log(dayArr, eddDate)
    }  
// 获取指定时间戳  当前 前  后  正月的时间
 function   getMonthdays(timestamp) {
      let MonthDate = new Date(timestamp)
      const year = MonthDate.getFullYear()
      const month =
        MonthDate.getMonth() + 1 < 10
          ? `0${MonthDate.getMonth() + 1}`
          : MonthDate.getMonth() + 1
      let dayArr = []
      let eddDate = new Date(year, MonthDate.getMonth() + 1, 0).getDate()

      for (let i = 1; i <= eddDate; i++) {
        dayArr.push(`${year}-${month}-${i < 10 ? `0${i}` : i}`)
      }
      return dayArr
    }

   function getmonths(timestamp) {
      let MonthDate = new Date(timestamp)
      const year = MonthDate.getFullYear()
      const month = MonthDate.getMonth() + 1 
      let monthNum = new Date(year, month, 0)
      let starstamp = new Date(`${year}/${month}/1`).getTime()
      var timeEnd = Date.parse(monthNum)
      let boeforeMonthday = starstamp - 86400000
      let afterMonthday = timeEnd + 86400000
      const arr = [boeforeMonthday, timeEnd, afterMonthday]
      let endVal = {}
      for (let i = 0; i <= arr.length - 1; i++) {
        endVal[i] = getMonthdays(arr[i])
      }  console.log(endVal)
    }    

你可能感兴趣的:(js,javascript,前端,开发语言)