關注公眾號: 微信搜索 `前端工具人 ; 收貨更多的干貨
開發日常,沒什么技巧,主要是換算,直接看代碼;復制粘貼即可用
對應的是element-ui DatePicker
帶快捷選項 時間控件
,所以返回的是數組;
export const getTime = function () {
// 起止日期數組
let dateTime = {
// 昨天
yesterday: [],
// 今天
today: [],
// 上一周
lastWeekDate: [],
// 本周
thisWeekDate: []
}
// 獲取當前時間
let currentDate = new Date()
// 返回date是一周中的某一天
let week = currentDate.getDay()
// 返回date是一個月中的某一天
// let month = currentDate.getDate()
// 一天的毫秒數
let millisecond = 1000 * 60 * 60 * 24
// 減去的天數
let minusDay = week !== 0 ? week - 1 : 6
// 獲得當前周的第一天
let currentWeekDayOne = new Date(currentDate.getTime() - (millisecond * minusDay))
// 上周最后一天即本周開始的前一天
let priorWeekLastDay = new Date(currentWeekDayOne.getTime() - millisecond)
// 上周的第一天
let priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (millisecond * 6))
// 上周第一天時間封裝
const start = `${priorWeekFirstDay.getFullYear()}-${supplement(priorWeekFirstDay.getMonth() + 1, 2)}-${supplement(priorWeekFirstDay.getDate(), 2)} 00:00:00`
// 上周最后一天時間封裝
const end = `${priorWeekLastDay.getFullYear()}-${supplement(priorWeekLastDay.getMonth() + 1, 2)}-${supplement(priorWeekLastDay.getDate(), 2)} 23:59:59`
// 本周 周一
let monday = new Date(currentDate.getTime() - (minusDay * millisecond))
// 本周 周日
let sunday = new Date(monday.getTime() + (6 * millisecond))
// 本周第一天時間封裝
const start1 = `${monday.getFullYear()}-${supplement(monday.getMonth() + 1, 2)}-${supplement(monday.getDate(), 2)} 00:00:00`
// 本周最后一天時間封裝
const end2 = `${sunday.getFullYear()}-${supplement(sunday.getMonth() + 1, 2)}-${supplement(sunday.getDate(), 2)} 23:59:59`
// 添加至上周數組
dateTime.lastWeekDate.push(start, end)
// 添加至本周數組
dateTime.thisWeekDate.push(start1, end2)
// 昨天
dateTime.yesterday.push(`${currentDate.getFullYear()}-${supplement(currentDate.getMonth() + 1, 2)}-${supplement(currentDate.getDate() - 1, 2)} 00:00:00`, `${currentDate.getFullYear()}-${supplement(currentDate.getMonth() + 1, 2)}-${supplement(currentDate.getDate() - 1, 2)} 23:59:59`)
// 今天
dateTime.today.push(`${currentDate.getFullYear()}-${supplement(currentDate.getMonth() + 1, 2)}-${supplement(currentDate.getDate(), 2)} 00:00:00`, `${currentDate.getFullYear()}-${supplement(currentDate.getMonth() + 1, 2)}-${supplement(currentDate.getDate(), 2)} 23:59:59`)
return dateTime
}