XypriL
XypriL

Reputation: 179

How to get the last 7 days from now using Moment.js?

So, I have the following data in UTC format.

0: "2021-07-29T09:55:16.475Z"
1: "2021-09-01T13:21:02.652Z"
2: "2021-08-27T09:55:16.475Z"
3: "2021-08-30T13:21:02.652Z"
4: "2021-08-29T09:55:16.475Z"

And what I want is to get only the last 7 days from now and based from the given data above, it should not return the first data. For now, I only have this code which helps me to format dates and I can't seem to think on how can I achieve what I want. Thanks in advance.

 const date = concerns.map((concern) =>
    moment(concern.dateCreated).format("LL")
  );

Upvotes: 0

Views: 1045

Answers (1)

vaira
vaira

Reputation: 2270

const cutOffDate = moment().subtract(7, 'days');
const dates = concerns.filter((concern) => moment(concern.dateCreated) > cutOffDate  
).map((concern) => moment(concern.dateCreated).format("LL"));

Upvotes: 2

Related Questions