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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:57:17  来源:igfitidea点击:

How to calculate the date 30 days from today

javascriptdate

提问by HymanTurky

i need to calculate the date from thirty days using Datein javascript

我需要使用Datein计算三十天的日期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

I think its better for you to use Datejs

我认为你使用Datejs更好

Datejs is an open-source JavaScript Date Library.

Datejs 是一个开源的 JavaScript 日期库。

or you can do it own:

或者你可以自己做:

var cur = new Date(),
    after30days = cur.setDate(cur.getDate() + 30);

回答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)