0
点赞
收藏
分享

微信扫一扫

(学习笔记)数据基建-数据质量

 获取当前周周一和周日的日期(格式20240607)

function getWeekStartAndEndDate() {
    const today = new Date() // 获取当前日期
    const dayOfWeek = today.getDay() // 获取今天是周几,返回值是0(周日)到6(周六)

    // 获取本周周一的日期
    const monday = new Date(today)
    monday.setDate(today.getDate() - dayOfWeek + 1) // 减去今天是周几的天数

    // 获取本周周日的日期
    const sunday = new Date(monday)
    sunday.setDate(sunday.getDate() + 6) // 周一加上6天得到周日

    // 格式化日期为 YYYY-MM-DD 格式
    function formatDate(date) {
      const year = date.getFullYear()
      const month = (date.getMonth() + 1).toString().padStart(2, '0')
      const day = date.getDate().toString().padStart(2, '0')
      return `${year}${month}${day}`
    }

    return {
      monday: formatDate(monday),
      sunday: formatDate(sunday)
    }
  }

  const { monday, sunday } = getWeekStartAndEndDate()

向大家tui键一个有趣的gong众hao,记得关注哦! 

举报

相关推荐

0 条评论