使用 moment js nodejs 将日期推送到数组
Push a date to an array with moment js nodejs
我正在 NodeJs 中编写一个函数,其中获取 2 个日期 returns 一个包含范围内所有日期的数组。
相反,我得到一个数组,该数组每次都会添加相同的日期并自行删除。
例如:获取开始日期 1/11/2026 和结束日期 3/11/2026 它应该 return [1/11/2026, 2/11/2026, 3/11/2026]
现在它:
[1/11/2026]、[2/11/2026、2/11/2026]、[3/11/2026、3/11/2026、3/11/2026]
我尝试从 while 格式中移出,但我将其转换为字符串
我尝试使用 2 个数组并使用扩展运算符但没有成功
这是我的代码:
const dateList = ({ start, end }) => {
let now = moment(start);
const end = moment(end);
let dates = [];
while (now.format('YYYY-MM-DD') <= end.format('YYYY-MM-DD')) {
dates.push(now);
now = now.add(1, 'days');
}
return dates;
}
瞬间使用对象实例
你的变量 now
是时刻日期的一个实例,所以如果你将它推入你的数组并将他的日期设置为 +1,你的数组中的实例也会得到这个更改,因为它是同一个实例。您需要推送 moment 实例的克隆。
const dateList = ({ start, end }) => {
let now = moment(start);
const end = moment(end);
let dates = [];
while (now.format('YYYY-MM-DD') <= end.format('YYYY-MM-DD')) {
dates.push(now.clone());
now = now.add(1, 'days');
}
return dates;
}
并且你可以使用 moment 查询来测试 end
是否在 now
.
之后
while(now.format('YYYY-MM-DD') <= end.format('YYYY-MM-DD'))
=> while(now.isSameOrBefore(end))
最终代码:
const dateList = ({ start, end }) => {
let now = moment(start);
const end = moment(end);
let dates = [];
while (now.isSameOrBefore(end)) {
dates.push(now.clone());
now = now.add(1, 'days');
}
return dates;
}
const dateList = ({ start, end }) => {
let now = moment.utc(start);
end = moment.utc(end);
let dates = [];
while (now.isValid() && end.isValid() && now.isSameOrBefore(end, 'day') /* end day inclusive */) {
dates.push(now);
now = moment(now).add(1, 'days'); // add 1 day on a new instance
}
return dates;
}
console.log(dateList({start: '2026-05-12', end: '2026-05-14'}))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
我正在 NodeJs 中编写一个函数,其中获取 2 个日期 returns 一个包含范围内所有日期的数组。 相反,我得到一个数组,该数组每次都会添加相同的日期并自行删除。
例如:获取开始日期 1/11/2026 和结束日期 3/11/2026 它应该 return [1/11/2026, 2/11/2026, 3/11/2026] 现在它: [1/11/2026]、[2/11/2026、2/11/2026]、[3/11/2026、3/11/2026、3/11/2026]
我尝试从 while 格式中移出,但我将其转换为字符串 我尝试使用 2 个数组并使用扩展运算符但没有成功
这是我的代码:
const dateList = ({ start, end }) => {
let now = moment(start);
const end = moment(end);
let dates = [];
while (now.format('YYYY-MM-DD') <= end.format('YYYY-MM-DD')) {
dates.push(now);
now = now.add(1, 'days');
}
return dates;
}
瞬间使用对象实例
你的变量 now
是时刻日期的一个实例,所以如果你将它推入你的数组并将他的日期设置为 +1,你的数组中的实例也会得到这个更改,因为它是同一个实例。您需要推送 moment 实例的克隆。
const dateList = ({ start, end }) => {
let now = moment(start);
const end = moment(end);
let dates = [];
while (now.format('YYYY-MM-DD') <= end.format('YYYY-MM-DD')) {
dates.push(now.clone());
now = now.add(1, 'days');
}
return dates;
}
并且你可以使用 moment 查询来测试 end
是否在 now
.
while(now.format('YYYY-MM-DD') <= end.format('YYYY-MM-DD'))
=> while(now.isSameOrBefore(end))
最终代码:
const dateList = ({ start, end }) => {
let now = moment(start);
const end = moment(end);
let dates = [];
while (now.isSameOrBefore(end)) {
dates.push(now.clone());
now = now.add(1, 'days');
}
return dates;
}
const dateList = ({ start, end }) => {
let now = moment.utc(start);
end = moment.utc(end);
let dates = [];
while (now.isValid() && end.isValid() && now.isSameOrBefore(end, 'day') /* end day inclusive */) {
dates.push(now);
now = moment(now).add(1, 'days'); // add 1 day on a new instance
}
return dates;
}
console.log(dateList({start: '2026-05-12', end: '2026-05-14'}))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>