moment时间戳转日期=> moment.unix(时间戳).format(“YYYY-MM-DD”)
//先安装moment
//npm i moment
//再安装Lodash,可以用它内部封装好的方法,简单实用
//npm i lodash
const moment = require("moment");
const _ = require("lodash");
//假设需要查询2019-06-25至2019-12-31号之间每一天的00:00至23:59这个时间段的数据
//将时间转换为moment时间格式
const start = "2019-06-25";//开始时间
const start_time = moment(start);
console.log(moment(start_time));//moment("2019-06-25T00:00:00.000")
const end = "2019-12-31";
const end_time = moment(end).endOf("d");
console.log(end_time);//endOf("d")为当天的结束时间 moment("2019-12-31T23:59:59.999")
//开始计算这两个时间段相差的天数
const diff_times = end_time.diff(start_time,"d");
console.log(diff_times);//189
//lodash中内置的循环器,可以指定循环次数
//再定义一个数组,用来存放相差的每一天日期
const arr = [];
_.times(diff_times, i => {
const new_start_time = moment(start_time);//每次重新初始化开始时间,因为我碰到了深拷贝的问题
arr.push(new_start_time.add(i, "days").format("YYYY-MM-DD"));//数组下标从0开始,可以用它进行每次的天数递增
});
//console.log(arr);
/**
* [ '2019-06-25',
'2019-06-26',
'2019-06-27',
'2019-06-28',
'2019-06-29',
'2019-06-30',
'2019-07-01',
'2019-07-02',
'2019-07-03',
'2019-07-04',
'2019-07-05',
'2019-07-06',
'2019-07-07',
'2019-07-08',
'2019-07-09',
'2019-07-10',
'2019-07-11',
'2019-07-12',
'2019-07-13'
... more items ]
*/
//现在我们已经获取了每一天的具体日期,接下来我们就可以对这每一年的日期进行头尾运算了
//再定义一个map循环用来存储每一天的开始和结束时间的十位时间戳
const map = _.map(arr, v => {
return {
time: v,
start_unix: moment(v).startOf('d').unix(),
end_unix : moment(v).endOf('d').unix()
}
});
//console.log(JSON.stringify(map));
/**
* [
* {"time":"2019-06-25","start_unix":1561392000,"end_unix":1561478399},
* {"time":"2019-06-26","start_unix":1561478400,"end_unix":1561564799},
* {"time":"2019-06-27","start_unix":1561564800,"end_unix":1561651199},
* {"time":"2019-06-28","start_unix":1561651200,"end_unix":1561737599},
* {"time":"2019-06-29","start_unix":1561737600,"end_unix":1561823999},
* {"time":"2019-06-30","start_unix":1561824000,"end_unix":1561910399},
* ..... more times ]
*/
//至此已经完成该时间段每天的头尾时间转换了
//如果需要获取某一年每个月的开始和结束时间呢?
//我们同样的以2019-06-25这个日期为例
const year = start.substring(0, 4);//取年份
const clone_year = start.substring(0, 4);//再取一个年份避免moment循环引用自身
//一年有12个月,在这里我们就直接循环十二次了
//定一个用于存放月份日期的数组
const arr_month = [];
_.times(12, i => {
//moment(`${i+1}/${year}`, "DD/YYYY")
//这样做的避免了JS时间的偏移 => https://momentjs.com/guides/#/warnings/js-date/
arr_month.push(
{
month : `${i+1} 月份`,
start_month: moment(`${i+1}/${year}`, "MM/YYYY").add(0, "M").format("YYYY-MM-DD"),
end_month : moment(`${i+1}/${clone_year}`, "MM/YYYY").endOf("M").add(0, "M").format("YYYY-MM-DD")
}
);
});
//console.log(arr_month);
/**
* [ { month: '1 月份',
start_month: '2019-01-01',
end_month: '2019-01-31' },
{ month: '2 月份',
start_month: '2019-02-01',
end_month: '2019-02-28' },
{ month: '3 月份',
start_month: '2019-03-01',
end_month: '2019-03-31' },
{ month: '4 月份',
start_month: '2019-04-01',
end_month: '2019-04-30' },
{ month: '5 月份',
start_month: '2019-05-01',
end_month: '2019-05-31' },
{ month: '6 月份',
start_month: '2019-06-01',
end_month: '2019-06-30' },
{ month: '7 月份',
start_month: '2019-07-01',
end_month: '2019-07-31' },
{ month: '8 月份',
start_month: '2019-08-01',
end_month: '2019-08-31' },
{ month: '9 月份',
start_month: '2019-09-01',
end_month: '2019-09-30' },
...... more times]
*/
//现在我们已经得到某一年每个月份的开始和结束时间了,我们还需要再进行时间戳转换
const map_month = _.map(arr_month, v => {
return {
month : v.month,
start_month_unix: moment(v.start_month).startOf('d').unix(),
end_month_unix : moment(v.end_month).endOf('d').unix()
};
});
console.log(map_month);
/**
* [ { month: '1 月份',
start_month_unix: 1546272000,
end_month_unix: 1548950399 },
{ month: '2 月份',
start_month_unix: 1548950400,
end_month_unix: 1551369599 },
{ month: '3 月份',
start_month_unix: 1551369600,
end_month_unix: 1554047999 },
{ month: '4 月份',
start_month_unix: 1554048000,
end_month_unix: 1556639999 },
{ month: '5 月份',
start_month_unix: 1556640000,
end_month_unix: 1559318399 },
{ month: '6 月份',
start_month_unix: 1559318400,
end_month_unix: 1561910399 },
{ month: '7 月份',
start_month_unix: 1561910400,
end_month_unix: 1564588799 },
{ month: '8 月份',
start_month_unix: 1564588800,
end_month_unix: 1567267199 },
....... more times]
*/