使用moment获取时间区间(今日,明日,本周,上周,下个月等)

直接去官方下载nodejs,它自带npm包管理工具
这个是下载node的地址

然后我们进行安装,并使用npm下载moment

npm i moment 默认下载最新版

在这里我们只精确到十位数,也就是秒

const moment = require('moment');
console.log(moment().startOf('day').unix());//今天 00:00
console.log(moment().endOf('day').unix() + '\n');
//明天后天和昨天前天直接加上86400即可 一天的时间是86400秒
let tmp = Date.parse(new Date()).toString();
let tmp2 = parseInt(tmp.substr(0, 10));//今天
console.log(tmp2,'今天'+'\n');
//86400000
//上周第一天
console.log(moment().week(moment().week() - 1).startOf('week').unix());
//上周最后一天
console.log(moment().week(moment().week() - 1).endOf('week').unix() + '\n');

//本周第一天
console.log(moment().week(moment().week()).startOf('week').unix(),'本周第一天');
//本周最后一天
console.log(moment().week(moment().week()).endOf('week').unix() + '\n');

//下周第一天
console.log(moment().week(moment().week() + 1).startOf('week').unix());
//下周最后一天
console.log(moment().week(moment().week() + 1).endOf('week').unix() + '\n');

//上个月第一天
console.log(moment().month(moment().month() - 1).startOf('month').unix());
//上个月最后一天
console.log(moment().month(moment().month() - 1).endOf('month').unix() + '\n');

//本月第一天
console.log(moment().month(moment().month()).startOf('month').unix());
//本月最后一天
console.log(moment().month(moment().month()).endOf('month').unix() + '\n');

//下个月第一天
console.log(moment().month(moment().month() + 1).startOf('month').unix(),'下个月');
//下个月最后一天
console.log(moment().month(moment().month() + 1).endOf('month').unix() + '\n');

//执行结果

1552924800
1553011199

1552982519 '今天\n'
1552147200
1552751999

1552752000 '本周第一天'
1553356799

1553356800
1553961599

1548950400
1551369599

1551369600
1554047999

1554048000 '下个月'
1556639999

猜你喜欢

转载自blog.csdn.net/qq_42427109/article/details/88664701