[英]Add A Year To Today's Date
I am trying to add a year to todays date.我正在尝试将今天的日期增加一年。 I am working in a system that does not allow you to use standard
JavaScript.我在不允许您使用standard
JavaScript 的系统中工作。
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?如果是这样,我怎么能增加一年呢?
You can create a new date object with todays date using the following code:您可以使用以下代码创建具有今天日期的新日期对象:
var d = new Date(); console.log(d);
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 上阅读有关日期对象的方法的更多信息
Use the Date.prototype.setFullYear method to set the year to what you want it to be.使用Date.prototype.setFullYear方法将年份设置为您想要的年份。
For example:例如:
const aYearFromNow = new Date(); aYearFromNow.setFullYear(aYearFromNow.getFullYear() + 1); console.log(aYearFromNow);
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 中确实没有其他方法可以处理日期。
One liner as suggested here此处建议的一个班轮
How to determine one year from now in Javascript by 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))
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)
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 timestamp of +1 year in a single line如果您想在一行中获取 +1 年的时间戳
console.log( (d => d.setFullYear(d.getFullYear() + 1))(new Date) )
If you want to get Date object with single line如果你想用单行获取日期对象
console.log( (d => new Date(d.getFullYear() + 1, d.getMonth(), d.getDate()))(new Date) )
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);
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
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
//This piece of code will handle the leap year addition as well. //这段代码也将处理闰年加法。
function updateExpiryDate(controlID, value) {
if ( $("#ICMEffectiveDate").val() != '' &&
$("#ICMTermYears").val() != '') {
var effectiveDate = $("#ICMEffectiveDate").val();
var date = new Date(effectiveDate);
var termYears = $("#ICMTermYears").val();
date = new Date(date.setYear(date.getFullYear() + parseInt(termYears)));
var expiryDate = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
$('#ICMExpiryDate').val(expiryDate);
}
}
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;
var yearsToAdd = 5;
var current = new Date().toISOString().split('T')[0];
var addedYears = Number(this.minDate.split('-')[0]) + yearsToAdd + '-12-31';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.