js获取两个时间戳之间的日期
function timestamp2Date(time) {
let date = new Date(time);
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day;
return year + '-' + month + '-' + day;
}
function getDate(datestr) {
var temp = datestr.split("-");
var date = new Date(temp[0], temp[1], temp[2]);
return date;
}
function getTimeStampBetween(start, end) {
var startTime = getDate(start);
var endTime = getDate(end);
var list = [];
while ((endTime.getTime() - startTime.getTime()) >= 0) {
var year = startTime.getFullYear();
var month = startTime.getMonth().toString().length == 1 ? "0" + startTime.getMonth().toString() : startTime.getMonth();
var day = startTime.getDate().toString().length == 1 ? "0" + startTime.getDate() : startTime.getDate();
list.push(year + "-" + month + "-" + day)
startTime.setDate(startTime.getDate() + 1);
}
return list;
}
var start = timestamp2Date(1646064000000);//2022-03-01
var end = timestamp2Date(1647878400000);//2022-03-22
var list=getTimeStampBetween(start,end);
console.log(list)
</script>
demo地址:
版权声明:若无特殊注明,本文皆为《菜鸟站长》原创,转载请保留文章出处。
本文链接:js获取两个时间戳之间的日期 - https://wziyi.com.cn/?post=336