js获取一周从开始到结束日期范围
生活随笔
收集整理的這篇文章主要介紹了
js获取一周从开始到结束日期范围
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 以周一作為周開始版本
const getWeekDate = (year, week) => {//獲取周開始日期const getWeekStartDate = (year, week) => {// Thursday in current week decides the year.const date = new Date(year, 0, 4);date.setDate(date.getDate() - (date.getDay() + 6) % 7);date.setTime(date.getTime() + 7 * 86400000 * (week - 1));const y = date.getFullYear();const m = date.getMonth() + 1;const d = date.getDate();return y + '/' + m + '/' + d;}//獲取周結束日期const getWeekEndDate = (year, week) => {// Thursday in current week decides the year.const date = new Date(year, 0, 4);date.setDate(date.getDate() - (date.getDay() + 6) % 7);date.setTime(date.getTime() + (7 * 86400000 * week) - 1);const y = date.getFullYear();const m = date.getMonth() + 1;const d = date.getDate();return y + '/' + m + '/' + d ;}return getWeekStartDate(year, week) + '-' + getWeekEndDate(year, week); }2. 以周日作為周開始版本
function getWeekDate(year, week) {//獲取周開始日期const getWeekStartDate = (year, week) => {// Thursday in current week decides the year.const date = new Date(year, 0, 4);date.setDate(date.getDate() - date.getDay());date.setTime(date.getTime() + 7 * 86400000 * (week - 1));const y = date.getFullYear();const m = date.getMonth() + 1;const d = date.getDate();return y + '/' + m + '/' + d;}//獲取周結束日期const getWeekEndDate = (year, week) => {// Thursday in current week decides the year.const date = new Date(year, 0, 4);date.setDate(date.getDate() - date.getDay());date.setTime(date.getTime() + (7 * 86400000 * week) - 1);const y = date.getFullYear();const m = date.getMonth() + 1;const d = date.getDate();return y + '/' + m + '/' + d;}return getWeekStartDate(year, week) + '-' + getWeekEndDate(year, week); }?
總結
以上是生活随笔為你收集整理的js获取一周从开始到结束日期范围的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js实现获取当前周,过去和未来周的时间段
- 下一篇: Git安装及配置