Vince Carter
Vince Carter

Reputation: 946

Moment.js returns false date

I am using moment.js in one React native application.

In one component I am generating a calendar, with three days before and after today:

const cachedTime = new Date().getTime();
return [-2, -1, 0, 1, 2, 3, 4].map(val => {
    const rowDate = new Date(cachedTime + 24 * 60 * 60 * val * 1000);
    console.log(Moment(rowDate).format('YYYY-MM-DD'));
}

Maybe because of the time change it displays one day two times:

2020-10-23
2020-10-24
2020-10-25
2020-10-25
2020-10-26
2020-10-27
2020-10-28

How can I change this?

Upvotes: 0

Views: 36

Answers (1)

Mehran Khan
Mehran Khan

Reputation: 3636

You can use "add" function of moment library

try this

function getDates() {
  return [-3, -2, -1, 0, 1, 2, 3].map(val => {
      return moment().add(val,"days").format('YYYY-MM-DD');
  })
}

const dates = getDates();
console.log(dates) 

enter image description here

Upvotes: 1

Related Questions