JS Date操作

JS Date操作

1
const weekDay = new Date(date.setDate(1)).getDay() // 这个月的第一天是星期几const monthLen = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate() // 本月一共多少天

获取一周的日期

1
2
3
getDate(offset: number) {
const current = new Date(); const weekDay = current.getDay(); const dayMilliseconds = 24 * 60 * 60 * 1000; // 一天多少毫秒 const flagTimestamp = current.setHours(0, 0, 0, 0) + offset * 7 * dayMilliseconds; const dateAry = [...new Array(7).keys()]
.map((i: number) => new Date(flagTimestamp + (i + 1 - weekDay) * dayMilliseconds)); console.log(dateAry);}