如何从普通日期中减去天数? [英] How to subtract days from a plain Date?

查看:76
本文介绍了如何从普通日期中减去天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法来获取原始的JavaScript Date (例如今天)并返回X天?

Is there an easy way of taking a olain JavaScript Date (e.g. today) and going back X days?

例如,如果我想计算今天前5天的日期.

So, for example, if I want to calculate the date 5 days before today.

推荐答案

尝试如下操作:

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

请注意,这会修改日期对象并返回更新日期的时间值.

Note that this modifies the date object and returns the time value of the updated date.

var d = new Date();

document.write('Today is: ' + d.toLocaleString());

d.setDate(d.getDate() - 5);

document.write('<br>5 days ago was: ' + d.toLocaleString());

这篇关于如何从普通日期中减去天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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