我主要使用了[moment.js]插件对这些时间进行了整理,moment的安装过程点击[这里]
话不多说,直接上代码
`date`为当前时间,如`"2022-01-03 15:31:21"`
**当前天**
```javascript
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”]`
**当前周**
```javascript
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”]`
**当前月**
```javascript
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”]`
**当前年**
```javascript
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”]`
**前一天**
```javascript
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”]`
**后一天**
```javascript
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”]`
**上周**
```javascript
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`进行输入
**下周**
```javascript
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”]`
**上月**
```javascript
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”]`
**下月**
```javascript
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”]`
**上年**
```javascript
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”]`
**下年**
```javascript
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”]`
好用的话,点个赞呗!