在Javascript中减少1天的最简单方法是什么? [英] What's the simplest way to decrement a date in Javascript by 1 day?

查看:82
本文介绍了在Javascript中减少1天的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将JavaScript日期减1天,以便正确地在几个月/几年内回滚。也就是说,如果我的日期是今天,我想要获得昨天的日期。



当我需要时,总是似乎需要更多的代码这样做,所以我想知道是否有一个简单的方法。



最简单的方法是什么?



解决方案

  var d = new Date(); d.setDate(d.getDate() -  1); console.log(d);  / pre> 


I need to decrement a Javascript date by 1 day, so that it rolls back across months/years correctly. That is, if I have a date of 'Today', I want to get the date for 'Yesterday'.

It always seems to take more code than necessary when I do this, so I wonder if there's a simple way.

What's the simplest way of doing this?

[Edit: Just to avoid confusion in an answer below, this is a JavaScript question, not a Java one.]

解决方案

var d = new Date();
d.setDate(d.getDate() - 1);

console.log(d);

这篇关于在Javascript中减少1天的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆