Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

momentjs - Create array of last 30 days

I am using momentjs and am trying to create an array containing the last 30 days.

I think I can do this be creating a counter and then counting backwards, creating a moment for each day.

But is this the best method? Does anybody know if momentjs has a function built in that can already do this?

like image 583
fightstarr20 Avatar asked Sep 03 '25 06:09

fightstarr20


1 Answers

As best I know, momentjs doesn't have any built in features for automatically creating an array containing moment instances, but you can easily construct an array of the previous thirty days using a short statement like this:

const lastThirtyDays = [...new Array(30)].map((i, idx) => moment().startOf("day").subtract(idx, "days"));

momentjs also has the ability to create objects that represent durations if that's of any help to you with your current problem.

like image 139
ajxs Avatar answered Sep 04 '25 22:09

ajxs