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

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

问题描述

我需要1天递减一个Javascript日期,因此,它回滚整个月/年正常。也就是说,如果我有'今天'的日期,我想要得到'昨天'的日期。

它似乎总是我这样做的时候采取更多code不是必须的,所以我不知道,如果有一个简单的方法。

什么是这样做的最简单的方法?


解决方案

 变种D =新的日期();
d.setDate(d.getDate() - 1);

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

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

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