开发者

how to get Previous Day?

I have date object:

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

How to get dateObj - 1 da开发者_StackOverflow社区y ?


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


By subtracting one day (1000 ms * 60 sec * 60 min * 24 hours):

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');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜