前段时间需要用到这些数据,在网上找了好久,感觉写的有点冗余,自己根据moment.js库整理了一下。我主要使用了moment.js时间管理库对这些时间进行了整理,moment的安装过程点击这里,有现成的 库为啥不用呢
话不多说,直接上代码
date
为当前时间,如"2022-01-03 15:31:21"
当前天
nowDay(date) {
let startDate = moment(date).startOf("days").format("YYYY-MM-DD HH:mm:ss")
let endDate = moment(date).endOf("days").format("YYYY-MM-DD HH:mm:ss")
return [startDate, endDate]
}
输出:["2022-01-03 00:00:00",“2022-01-03 23:59:59”]
当前周
nowWeek(date) {
let startDate = moment(date).startOf("week").add(1, "days").format("YYYY-MM-DD HH:mm:ss")
let endDate = moment(date).endOf("week").add(1, "days").format("YYYY-MM-DD HH:mm:ss");
return [startDate, endDate]
}
输出:["2022-01-03 00:00:00",“2022-01-09 23:59:59”]
当前月
nowMonth(date) {
let startDate = moment(date).startOf("month").format("YYYY-MM-DD HH:mm:ss")
let endDate = moment(date).endOf("month").format("YYYY-MM-DD HH:mm:ss")
return [startDate, endDate]
},
输出:["2022-01-01 00:00:00",“2022-01-31 23:59:59”]
当前年
nowYear(date) {
let startDate = moment(date).startOf("year").format("YYYY-MM-DD HH:mm:ss")
let endDate = moment(date).endOf("year").format("YYYY-MM-DD HH:mm:ss")
return [startDate, endDate]
},
输出:["2022-01-02 00:00:00",“2022-12-31 23:59:59”]
前一天
preDay(date) {
let startDate = moment(date).subtract(1, "days").startOf("days").format("YYYY-MM-DD HH:mm:ss")
let endDate = moment(date).subtract(1, "days").endOf("days").format("YYYY-MM-DD HH:mm:ss")
return [startDate, endDate]
},
输出:["2022-01-02 00:00:00",“2022-01-02 23:59:59”]
后一天
nextDay(date) {
let startDate = moment(date).add(1, "days").startOf("days").format("YYYY-MM-DD HH:mm:ss")
let endDate = moment(date).add(1, "days").endOf("days").format("YYYY-MM-DD HH:mm:ss")
return [startDate, endDate]
},
输出:["2022-01-04 00:00:00",“2022-01-04 23:59:59”]
上周
preWeek(date) {
let startDate = moment(date).weekday(-7).startOf("week").add(1, "days").format("YYYY-MM-DD HH:mm:ss")
let endDate = moment(date).weekday(-7).endOf("week").add(1, "days").format("YYYY-MM-DD HH:mm:ss");
return [startDate, endDate]
},
输出:["2021-01-27 00:00:00",“2022-01-02 23:59:59”]
无限套娃可将得到的时间再次作为date
进行输入
下周
nextWeek(date) {
let startDate = moment(date).weekday(7).startOf("week").add(1, "days").format("YYYY-MM-DD HH:mm:ss")
let endDate = moment(date).weekday(7).endOf("week").add(1, "days").format("YYYY-MM-DD HH:mm:ss");
return [startDate, endDate]
},
输出:["2022-01-10 00:00:00",“2022-01-16 23:59:59”]
上月
preMonth(date) {
let startDate = moment(date).subtract(1, "month").startOf("month").format("YYYY-MM-DD HH:mm:ss")
let endDate = moment(date).subtract(1, "month").endOf("month").format("YYYY-MM-DD HH:mm:ss")
return [startDate, endDate]
},
输出:["2021-12-01 00:00:00",“2021-12-31 23:59:59”]
下月
nextMonth(date) {
let startDate = moment(date).add(1, "month").startOf("month").format("YYYY-MM-DD HH:mm:ss")
let endDate = moment(date).add(1, "month").endOf("month").format("YYYY-MM-DD HH:mm:ss")
return [startDate, endDate]
},
输出:["2022-02-01 00:00:00",“2022-02-28 23:59:59”]
上年
preYear(date) {
let startDate = moment(date).subtract(1, "year").startOf("year").format("YYYY-MM-DD HH:mm:ss")
let endDate = moment(date).subtract(1, "year").endOf("year").format("YYYY-MM-DD HH:mm:ss")
return [startDate, endDate]
},
输出:["2021-01-31 00:00:00",“2021-12-31 23:59:59”]
下年
nextYear(date) {
let startDate = moment(date).add(1, "year").startOf("year").format("YYYY-MM-DD HH:mm:ss")
let endDate = moment(date).add(1, "year").endOf("year").format("YYYY-MM-DD HH:mm:ss")
return [startDate, endDate]
},
输出:["2023-01-31 00:00:00",“2023-12-31 23:59:59”]
好用的话,点个赞呗!