简体   繁体   中英

how to get Previous Day?

I have date object:

dateObj = new Date(year, month, date[, hours, minutes, seconds, ms] )

How to get dateObj - 1 day?

dateObj.setDate(dateObj.getDate()-1);

通过减去一天(1000 毫秒 * 60 秒 * 60 分钟 * 24 小时):

new Date(+new Date(year, month, date[, hours, minutes, seconds, ms] ) - 1000*60*60*24);

Some useful functions that doesnt mutate the orignal date object:

function prevDate(date) {
  let copy = new Date(date.valueOf());
  return new Date(copy.setDate(copy.getDate() - 1));
}

function nextDate(date) {
  let copy = new Date(date.valueOf());
  return new Date(copy.setDate(copy.getDate() + 1));
}

Have a look at the moment.js library https://momentjs.com/docs/

To get the date previous day you would simply need to do...

moment().add(-1, 'days');

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