小程序
小游戏
企业微信
微信支付
扫描小程序码分享
求救:需要获取 2019-12-20 到 2020-3-2 号之间所有日期的列表,结果像这样:[2019-12-20,2019-12-21,2019-12-22,......2020-3-2]。请问怎样才能实现呢?存在跨月跨年的问题。
我现在考虑 通过 new Date 获得当前日期,然后每次加1,但到2019-12-31号再加1,怎么能跳到 2020-1-1 而不能是2019-12-32 呢?
3 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
直接写
function time(time) {
var date = new Date(time || (new Date().getTime() + 8 * 3600 * 1000));
return date.toJSON().substr(0, 19).replace('T', ' ');
}
var start = '2019-10-01', end = '2022-10-01', st = new Date(start).getTime(), et = new Date(end).getTime(), dates = [], dayTimestamp = 86400 * 1000
for (var i = 0; i <= (et - st) / dayTimestamp; i++) {
dates.push(time(st + i * dayTimestamp).split(' ').shift())
console.log(res)
=============================================================
封装
var RangeDate = {
dates: [],
onlyDates: [],
onlyDate: !1,
callback: () => { },
dTimestamp: 86400 * 1000,
time(time) {
},
getRangeDateA(options) {
var st = new Date(options.sD).getTime(), et = new Date(options.eD).getTime()
this.onlyDate = options.onlyDate == !0 ? !0 : !1
this.callback = options.callback ? options.callback : this.callback
this.dates = []
this.onlyDates = []
for (var i = 0; i <= (et - st) / this.dTimestamp; i++) {
var tmp = this.time(st + i * this.dTimestamp)
this.dates.push(tmp)
this.onlyDates.push(tmp.split(' ').shift())
return this
getRangeDate(options) {
var t = this
return new Promise((rs, rj) => {
var d = t.getRangeDateA(options).result()
this.callback(d)
rs(d)
})
result() {
return this.onlyDate ? this.onlyDates : this.dates
==================================================
使用例子
链式风格
console.log(RangeDate.getRangeDateA({
sD: '2019-10-01',
eD: '2022-10-01',
onlyDate: !0
}).result()
)
callback风格:
RangeDate.getRangeDate({
onlyDate: !0,
callback: function (res) {
Promise风格
}).then(res => {
async / await 风格
console.log(await RangeDate.getRangeDate({
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
// startDate: 计划开始时间; endDate:计划结束时间;dayLength:每隔几天,0-代表每天,1-代表日期间隔一天
const
getDates = (startDate, endDate, dayLength) => {
let result = [];
result.push(startDate);
for
(let i =
0
; ; i++) {
let getDate = getTargetDate(startDate, dayLength);
startDate = getDate;
if
(getDate <= endDate) {
result.push(getDate);
else
{
break
;
return
result;
// startDate: 开始时间;dayLength:每隔几天,0-代表获取每天,1-代表日期间隔一天
getTargetDate = (date, dayLength) => {
dayLength = dayLength +
1
let tempDate =
new
Date
(date);
tempDate.setDate(tempDate.getDate() + dayLength);
let year = tempDate.getFullYear();
let month = tempDate.getMonth() +
<
10
?
"0"
+ (tempDate.getMonth() +
) : tempDate.getMonth() +
let day = tempDate.getDate() <
+ tempDate.getDate() : tempDate.getDate();
year +
"-"
+ month +
+ day;
module.exports = {
getDates: getDates
console.log(util.getDates(
"2019-12-20"
,
"2020-03-02"
));
网上找了这个轮子,封装了一下,测试可用
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
直接写
function time(time) {
var date = new Date(time || (new Date().getTime() + 8 * 3600 * 1000));
return date.toJSON().substr(0, 19).replace('T', ' ');
}
var start = '2019-10-01', end = '2022-10-01', st = new Date(start).getTime(), et = new Date(end).getTime(), dates = [], dayTimestamp = 86400 * 1000
for (var i = 0; i <= (et - st) / dayTimestamp; i++) {
dates.push(time(st + i * dayTimestamp).split(' ').shift())
}
console.log(res)
=============================================================
封装
var RangeDate = {
dates: [],
onlyDates: [],
onlyDate: !1,
callback: () => { },
dTimestamp: 86400 * 1000,
time(time) {
var date = new Date(time || (new Date().getTime() + 8 * 3600 * 1000));
return date.toJSON().substr(0, 19).replace('T', ' ');
},
getRangeDateA(options) {
var st = new Date(options.sD).getTime(), et = new Date(options.eD).getTime()
this.onlyDate = options.onlyDate == !0 ? !0 : !1
this.callback = options.callback ? options.callback : this.callback
this.dates = []
this.onlyDates = []
for (var i = 0; i <= (et - st) / this.dTimestamp; i++) {
var tmp = this.time(st + i * this.dTimestamp)
this.dates.push(tmp)
this.onlyDates.push(tmp.split(' ').shift())
}
return this
},
getRangeDate(options) {
var t = this
return new Promise((rs, rj) => {
var d = t.getRangeDateA(options).result()
this.callback(d)
rs(d)
})
},
result() {
return this.onlyDate ? this.onlyDates : this.dates
}
}
==================================================
使用例子
链式风格
console.log(RangeDate.getRangeDateA({
sD: '2019-10-01',
eD: '2022-10-01',
onlyDate: !0
}).result()
)
callback风格:
RangeDate.getRangeDate({
sD: '2019-10-01',
eD: '2022-10-01',
onlyDate: !0,
callback: function (res) {
console.log(res)
}
})
Promise风格
RangeDate.getRangeDate({
sD: '2019-10-01',
eD: '2022-10-01',
onlyDate: !0
}).then(res => {
console.log(res)
})
async / await 风格
console.log(await RangeDate.getRangeDate({
sD: '2019-10-01',
eD: '2022-10-01',
onlyDate: !0
})
)
// startDate: 计划开始时间; endDate:计划结束时间;dayLength:每隔几天,0-代表每天,1-代表日期间隔一天
const
getDates = (startDate, endDate, dayLength) => {
let result = [];
result.push(startDate);
for
(let i =
0
; ; i++) {
let getDate = getTargetDate(startDate, dayLength);
startDate = getDate;
if
(getDate <= endDate) {
result.push(getDate);
}
else
{
break
;
}
}
return
result;
}
// startDate: 开始时间;dayLength:每隔几天,0-代表获取每天,1-代表日期间隔一天
const
getTargetDate = (date, dayLength) => {
dayLength = dayLength +
1
;
let tempDate =
new
Date
(date);
tempDate.setDate(tempDate.getDate() + dayLength);
let year = tempDate.getFullYear();
let month = tempDate.getMonth() +
1
<
10
?
"0"
+ (tempDate.getMonth() +
1
) : tempDate.getMonth() +
1
;
let day = tempDate.getDate() <
10
?
"0"
+ tempDate.getDate() : tempDate.getDate();
return
year +
"-"
+ month +
"-"
+ day;
}
module.exports = {
getDates: getDates
}
console.log(util.getDates(
"2019-12-20"
,
"2020-03-02"
,
0
));
网上找了这个轮子,封装了一下,测试可用