如何获得前一天

how to get Previous Day?

本文关键字:前一天 何获得      更新时间:2023-09-26

我有日期对象:

dateObj = 新日期(年、月、日期[、小时、分钟、秒、毫秒] )

如何获得日期 - 1 天 ?

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

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

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

不会改变原始日期对象的有用函数:

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));
}

看看现在.js图书馆 https://momentjs.com/docs/

要获得前一天的日期,您只需要做...

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