简体   繁体   中英

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?

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) 

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM