vue中获取上周、本周、下周

425 阅读1分钟

//当前年、月、日

`const getNewDate = data => {

const date = new Date(data)
const year = date.getFullYear()
const month = date.getMonth()
const day = date.getDate()
return { year, month, day }

}`

// 是否是当月

`const isCurMonth = (currentYear, currentMonth, data) => {

  const { year, month } = getNewDate(data)

  const m = month

  return currentYear === year && m === currentMonth

}`

// 时间戳转日期

` function setDate(n) { return n < 10 ? '0' + n : n }

const timestampDate = data => { const time = new Date(data) const year = time.getFullYear() const month = time.getMonth() + 1 const day = time.getDate() return ${year}-${setDate(month)}-${setDate(day)} }`

// 日历显示

`const getDashCalender = data => {

//存放日期

  const calendatArr = []

//当前日期

  const currentFirstDay = new Date()

  // 获取当前天星期几

  const weekNum = currentFirstDay.getDay()

  const weekDay = weekNum > 0 ? weekNum : 7

  // 获取上周最后一天

  const startTime = currentFirstDay - (weekDay + 6) * 24 * 60 * 60 * 1000

//三周一共21天,循环21

  const monthDayNum = 21

  for (let i = 0; i < monthDayNum; i++) {

    const dataTime = new Date(startTime + i * 24 * 60 * 60 * 1000)

    const { year, month } = getNewDate(dataTime)

    const todata = timestampDate(dataTime)

    const noMonth = isCurMonth(data.year, data.month, dataTime)

    const day = dataTime.getDate()

    calendatArr.push({

      todata: todata,//年-月-日

      date: dataTime,//完整日期

      year: year,//年

      month: month + 1,//月

      day: day, //日

      chickMonth: noMonth, //是否当月的日期

    })

  }

  return calendatArr

}`