Javascript 如何获得前一天?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5960674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:40:56  来源:igfitidea点击:

how to get Previous Day?

javascript

提问by Bdfy

I have date object:

我有日期对象:

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

How to get dateObj - 1 day ?

如何获得 dateObj-1 天?

回答by Billy Moon

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

回答by Mark Kahn

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

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

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

回答by TomJerrum

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

看看 moment.js 库https://momentjs.com/docs/

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

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

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