admin管理员组文章数量:1295690
I want to get the last x
dates from the current date using Moment.js
.
const moment = require('moment');
let dates;
const dates = [];
const NUM_OF_DAYS = 12; // get last 12 dates.
for (let i = 0; i < NUM_OF_DAYS; i++) {
date = moment();
date.subtract(1, 'day').format('DD-MM-YYYY');
dates.push(date);
}
console.log(dates);
The result of this code is as follows:
[ moment("2018-07-09T11:40:04.663"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675") ]
This is the same date and not the previous of what I want for each iteration.
I know the issue is with the line date = moment()
because it is returning the current timestamp and not the previous at each step.
How can I fix this?
I want to get the last x
dates from the current date using Moment.js
.
const moment = require('moment');
let dates;
const dates = [];
const NUM_OF_DAYS = 12; // get last 12 dates.
for (let i = 0; i < NUM_OF_DAYS; i++) {
date = moment();
date.subtract(1, 'day').format('DD-MM-YYYY');
dates.push(date);
}
console.log(dates);
The result of this code is as follows:
[ moment("2018-07-09T11:40:04.663"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675"),
moment("2018-07-09T11:40:04.675") ]
This is the same date and not the previous of what I want for each iteration.
I know the issue is with the line date = moment()
because it is returning the current timestamp and not the previous at each step.
How can I fix this?
Share Improve this question asked Jul 10, 2018 at 6:30 Utkarsh BhattUtkarsh Bhatt 1,6062 gold badges16 silver badges24 bronze badges 02 Answers
Reset to default 6You are pushing same variable to array and always subtracting 1 from current moment.
Declare date
variable inside the loop and subtract i
for(...){
let date = moment();
date.subtract(i, 'day')
...
}
const dates = [];
const NUM_OF_DAYS = 12; // get last 12 dates.
for (let i = 0; i < NUM_OF_DAYS; i++) {
let date = moment();
date.subtract(i, 'day').format('DD-MM-YYYY');
dates.push(date);
}
console.log(dates);
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Try this
const moment = require('moment');
let dates;
const dates = [];
const NUM_OF_DAYS = 12; // get last 12 dates.
for (let i = 0; i < NUM_OF_DAYS; i++) {
let date = moment();
date.subtract(i, 'day').format('DD-MM-YYYY');
dates.push(date);
}
console.log(dates);
In your code you were looping through and only subtracting 1 each time and not actually updating the number each time which is why you were only getting the same date, I've updated your code so as it loops it will update the number of days
本文标签: javascriptGetting the last X days date with MomentjsStack Overflow
版权声明:本文标题:javascript - Getting the last X days date with Moment.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741623354a2388941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论