获取本周周一至周日所对应的日期,获取本周日期

先看效果图

ps:如果想把周日移到最后,自己通过数组方法去处理

ps:日期格式请自行配置,在forEach循环中

定义周对象
const dateFormat = [
    {
      week: '周日', // week
      date: 0 // 时间戳
    }, {
      week: '周一',
      date: 0
    }, {
      week: '周二',
      date: 0
    }, {
      week: '周三',
      date: 0
    }, {
      week: '周四',
      date: 0
    }, {
      week: '周五',
      date: 0
    }, {
      week: '周六',
      date: 0
    }
]
  // 获取今日周几
  const weekDay = new Date().getDay()
  // 获取今日时间戳
  const dayDate = new Date().getTime()
  
  // 周一 ~ 周六
  if (weekDay > 0) {
     // 给今日前的日期赋时间戳
     for (let index1 = 0; index1 < weekDay; index1++) {
       dateFormat[index1].date = dayDate - ((weekDay - index1) * 86400000)
     }
     // 给今日后的日期赋时间戳
     for (let index2 = 0; index2 < 7 - weekDay; index2++) {
       dateFormat[weekDay + index2].date = dayDate + ((index2) * 86400000)
     }
     // 给周日重新赋时间戳
     dateFormat[0].date = dateFormat[6].date + 86400000
   } else { 

  // 周日

     // 给今日前的日期赋时间戳
     for (let index1 = 0; index1 < 7; index1++) {
        dateFormat[index1].date = dayDate - ((7 - index1) * 86400000)
     }
     // 给周日重新赋时间戳
     dateFormat[0].date = dateFormat[6].date + 86400000
   }
  dateFormat.forEach(item => {
    // item.date = new Date(item.date).format('MM-dd') // 这里用了封装好的Date方法 请自行处理
  })

console.log(dateFormat,'dateFormat')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅哥大大怪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值