Javascript 将年份添加到今天的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33070428/
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-08-23 14:35:24  来源:igfitidea点击:

Add year to todays date

javascriptdate

提问by Code

I am trying to add a year to todays date. I am working in a system that does not allow you to use standardJavaScript.

我想在今天的日期上加一年。我在一个不允许您使用standardJavaScript的系统中工作。

For instance, to get todays date I have to use:

例如,要获得今天的日期,我必须使用:

javascript:now();

I have tried:

我试过了:

javascript:now(+1);

I have never seen this before, but am in need of adding one year to todays date...

我以前从未见过这个,但我需要在今天的日期上增加一年......

Has anyone seen getting current date this way before? And if so, how could I add a year?

有没有人以前见过这种方式获取当前日期?如果是这样,我怎么能增加一年?

回答by Jonah Williams

You can create a new date object with todays date using the following code:

您可以使用以下代码创建具有今天日期的新日期对象:

var d = new Date();
    console.log(d);
// => 2015 年 10 月 11 日星期日 14:46:51 GMT-0700 (PDT)

If you want to create a date a specific time, you can pass the new Date constructor arguments

如果要在特定时间创建日期,可以传递新的 Date 构造函数参数

 var d = new Date(2014);
    console.log(d)

// => Wed Dec 31 1969 16:00:02 GMT-0800 (PST)

If you want to take todays date and add a year, you can first create a date object, access the relevant properties, and then use them to create a new date object

如果要取今天的日期加上年份,可以先创建一个日期对象,访问相关的属性,然后使用它们来创建一个新的日期对象

var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth();
    var day = d.getDate();
    var c = new Date(year + 1, month, day);
    console.log(c);

// => Tue Oct 11 2016 00:00:00 GMT-0700 (PDT)

You can read more about the methods on the date object on MDN

您可以在 MDN 上阅读有关日期对象的方法的更多信息

Date Object

日期对象

回答by Jon Koops

Use the Date.prototype.setFullYearmethod to set the year to what you want it to be.

使用Date.prototype.setFullYear方法将年份设置为您想要的年份。

For example:

例如:

var aYearFromNow = new Date();
aYearFromNow.setFullYear(aYearFromNow.getFullYear() + 1);

There really isn't another way to work with dates in JavaScript if these methods aren't present in the environment you are working with.

如果您正在使用的环境中不存在这些方法,那么在 JavaScript 中确实没有其他方法可以处理日期。

回答by Szekelygobe

One liner as suggested here

此处建议的一个班轮

How to determine one year from now in Javascriptby JP DeVries

JP DeVries如何在 Javascript 中确定从现在开始的一年

new Date(new Date().setFullYear(new Date().getFullYear() + 1))

Or you can get the number of years from somewhere in a variable:

或者您可以从变量中的某处获取年数:

const nr_years = 3;
new Date(new Date().setFullYear(new Date().getFullYear() + nr_years))

回答by Lucas Vellido

This code adds the amount of years required for a date.

此代码添加日期所需的年数。

var d = new Date();
// => Tue Oct 01 2017 00:00:00 GMT-0700 (PDT)

var amountOfYearsRequired = 2;
d.setFullYear(d.getFullYear() + amountOfYearsRequired);
// => Tue Oct 01 2019 00:00:00 GMT-0700 (PDT)

回答by Md Shahriar

    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth();
    var day = d.getDate();

    var fulldate = new Date(year + 1, month, day);

    var toDate = fulldate.toISOString().slice(0, 10);

    $("#txtToDate").val(toDate);

    output : 2020-01-02

回答by DC-

I like to keep it in a single line, you can use a self calling function for this eg:

我喜欢将它保留在一行中,您可以为此使用自调用函数,例如:

If you want to get the timestampof +1 year in a single line

如果您想在一行中获取+1 年的时间戳

console.log(
  (d => d.setFullYear(d.getFullYear() + 1))(new Date)
)

If you want to get Dateobject with single line

如果你想用单行获取Date对象

console.log(
  (d => new Date(d.getFullYear() + 1, d.getMonth(), d.getDate()))(new Date)
)

回答by Agidigbi Ayodeji Victor

In Angular, This is how you Calculate Date

在 Angular 中,这就是你计算日期的方式

today = new Date();
year = this.today.getFullYear();
month = this.today.getMonth();
day = this.today.getDate();
//To go 18 years back
yearsBack18= new Date(this.year - 18, this.month, this.day);

//To go to same day next year
nextYear= new Date(this.year + 1, this.month, this.day);

回答by Phemelo Khetho

In case you want to add years to specific date besides today's date using either years, or months, or days. You can do the following

如果您想使用年、月或日将年份添加到今天的日期之外的特定日期。您可以执行以下操作

var christmas2000 = new Date(2000, 12, 25); 
christmas2000.setFullYear(christmas2000.getFullYear() + 5); // using year: next 5 years
christmas2000.setFullYear(christmas2000.getMonth() + 24); // using months: next 24 months
christmas2000.setFullYear(christmas2000.getDay() + 365); // using days: next 365 months

回答by Md Shahriar

Function

功能

var date = new Date();

var year = date.getFullYear();

var month = date.getMonth();

month = month + 1;

if (month < 10) {
    month = "0" + month;
} 

var day = date.getDate();

if (day < 10) {
    day = "0" + day;
} 

year = year + 1;

var FullDate =  year + '/' + month + '/' + day; 

回答by mluis

var yearsToAdd = 5;
var current = new Date().toISOString().split('T')[0];
var addedYears = Number(this.minDate.split('-')[0]) + yearsToAdd + '-12-31';