javascript 如何计算从今天起 30 天的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10759647/
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
How to calculate the date 30 days from today
提问by HymanTurky
i need to calculate the date from thirty days using Date
in javascript
我需要使用Date
in计算三十天的日期javascript
var now = new Date();
Example:
例子:
if today is the 13 February 2013, 30 days later is the 15 March 2013. so something that is different from 30DaysLaterMonth = ActualMonth+1.
如果今天是 2013 年 2 月 13 日,30 天后就是 2013 年 3 月 15 日。 30DaysLaterMonth = ActualMonth+1.
I hope my question is clear.. :) thanks everybody!
我希望我的问题很清楚.. :) 谢谢大家!
回答by thecodeparadox
回答by Kenny Thompson
var now = new Date();
now.setDate(now.getDate() + 30);
回答by Claudio Redi
var now = new Date();
var 30DaysLaterMonth = now.getDate() + 30;
回答by Ale? Kotnik
In native javascript, use Date.UTC(year, month, day)
to get the number of milliseconds from 1971-01-01. Than add days * (86400000) and create date from this value:
在原生 javascript 中,用于Date.UTC(year, month, day)
获取从 1971-01-01 开始的毫秒数。比添加天 * (86400000) 并从此值创建日期:
var date_one_ms = Date.UTC(2012, 05, 25);
var ms_in_day = 24*3600*1000; // 86400000;
var date_30_days_later = new Date(date_one_ms + 30 * ms_in_day);
回答by Naive
var d = new Date();
d.setDate(d.getDate() + 30);
回答by WasiF
Get last 30 days form today
今天获取过去 30 天的表格
let now = new Date()
console.log(now)
let last30days = new Date(now.setDate(now.getDate() - 30))
console.log(last30days)
Get next 30 days from today
从今天开始接下来的 30 天
let now = new Date()
console.log(now)
let next30days = new Date(now.setDate(now.getDate() + 30))
console.log(next30days)