从JavaScript中减去日期 [英] Subtract days from a date in JavaScript

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

问题描述

有人知道一个简单的约会方式(例如今天)和回去X天吗?

Does anybody know of an easy way of taking a 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());

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

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