js 根据今天日期计算出这周的日期?

比如今天是

我获取 今天是周几
new Date().getUTCDay()  0 表示周日
在获取今天的号
new Date().getUTCDate()  15号

我想拿到15号这里一周的所有号,当然明天是16号,拿到的一周又是不同的,这样我该怎么计算啊,求解答
阅读 2.9k
1 个回答

你这里一周是 周一到周日吗

var today = new Date()
var start = today.setDate(today.getDate() - (today.getDay() || 7) + 1)

得到周一的日期,然后for循环出周一到周日

const today = new Date(new Date().toLocaleDateString())
const start = new Date(today.setDate(today.getDate() - (today.getDay() || 7) + 1))
const week = [new Date(start), ...Array(6).fill().map(_ => new Date(start.setDate(start.getDate() + 1)))]

const today = new Date(new Date().toLocaleDateString())
const start = today.setDate(today.getDate() - (today.getDay() || 7) + 1)
const week = [...Array(7).keys()].map(i => new Date(start + 864e5 * i))
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题