UNPKG

web-utils-super

Version:

前端函数库

42 lines (39 loc) 1.28 kB
const formatTime = require('./formatTime') /** * @desc: 获取某周起止时间 * @param {Date | String} 年月日 '2022-2-2' | '2022/2/2' | '2022.2.2' | 不传默认是当天 * @return {Array} */ function getCurrentWeek(time) { // 起止日期数组 let startStop = new Array() // 获取当前时间 let currentDate = time ? new Date(time) : new Date() // 返回date是一周中的某一天 let week = currentDate.getDay() // 一天的毫秒数 let millisecond = 1000 * 60 * 60 * 24 // 减去的天数 let minusDay = week != 0 ? week - 1 : 6 // alert(minusDay); // 本周 周一 let monday = new Date(currentDate.getTime() - minusDay * millisecond) monday.setHours(0) monday.setMinutes(0) monday.setSeconds(0) monday.setMilliseconds(0) // 本周 周日 let sunday = new Date(monday.getTime() + 7 * millisecond) sunday.setHours(0) sunday.setMinutes(0) sunday.setSeconds(0) sunday.setMilliseconds(0) sunday = new Date(sunday.getTime() - 1) // 添加本周时间 startStop.push(formatTime(monday)) // 本周起始时间 // 添加本周最后一天时间 startStop.push(formatTime(sunday)) // 本周终止时间 // 返回 return startStop } module.exports = getCurrentWeek