如何在 Javascript 中确定从现在开始的一年
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8609261/
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 determine one year from now in Javascript
提问by Ian Davis
I'm trying to get one year from now's date, and it's not working.
我试图从现在的日期算起一年,但它不起作用。
JS:
JS:
var now = new Date();
var oneYr = new Date();
oneYr.setYear(now.getYear() + 1);
$("#yearFromNow").append(oneYr.toString());
var oneMonth = new Date();
oneMonth.setMonth(now.getMonth() + 1);
$("#monthFromNow").append(oneMonth.toString());
Output:
输出:
one mo. = Thu Dec 22 112 15:16:01 GMT-0500 (Eastern Standard Time)
one yr. = Sun Jan 22 2012 15:16:01 GMT-0500 (Eastern Standard Time)
The year has Dec 22 112
- ?? The month is correctly displaying Jan 22 2012
.
年有Dec 22 112
- ?? 月份显示正确Jan 22 2012
。
If you want to tinker with it, http://jsbin.com/alezaj/edit#javascript,html,live. This is in Chrome and Firefox.
如果您想修改它,请访问http://jsbin.com/alezaj/edit#javascript,html,live。这是在 Chrome 和 Firefox 中。
Thanks!
谢谢!
采纳答案by Pointy
You should use getFullYear()
instead of getYear()
. getYear()
returns the actual year minus 1900 (and so is fairly useless).
你应该使用getFullYear()
而不是getYear()
. getYear()
返回实际年份减去 1900(因此相当无用)。
Thus a date marking exactly one year from the present moment would be:
因此,从当前时刻开始正好一年的日期将是:
var oneYearFromNow = new Date();
oneYearFromNow.setFullYear(oneYearFromNow.getFullYear() + 1);
Note that the date will be adjusted if you do that on February 29.
请注意,如果您在 2 月 29 日这样做,日期将被调整。
Similarly, you can get a date that's a month from now via getMonth()
and setMonth()
. You don't have to worry about "rolling over" from the current year into the next year if you do it in December; the date will be adjusted automatically. Same goes for day-of-month via getDate()
and setDate()
.
同样,您可以通过getMonth()
和获得一个月后的日期setMonth()
。12月做就不用担心从当年“翻”到下一年;日期会自动调整。月中的某天通过getDate()
和 也是如此setDate()
。
回答by JP DeVries
This will create a Date
exactly one year in the future with just one line. First we get the fullYear
from a new Date
, increment it, set that as the year of a new Date
. You might think we'd be done there, but if we stopped it would return a timestamp, not a Date
object so we wrap the whole thing in a Date
constructor.
这将Date
在未来创建一个只有一条线的一年。首先,我们fullYear
从 a 中获取new Date
,增加它,将其设置为 a 的年份new Date
。你可能认为我们就到此为止了,但如果我们停止,它会返回一个时间戳,而不是一个Date
对象,所以我们将整个事情包装在一个Date
构造函数中。
new Date(new Date().setFullYear(new Date().getFullYear() + 1))
回答by pliashkou
As setYear()
is deprecated, correct variant is:
由于setYear()
已被弃用,正确的变种:
// plus 1 year
new Date().setFullYear(new Date().getFullYear() + 1)
// plus 1 month
new Date().setMonth(new Date().getMonth() + 1)
// plus 1 day
new Date().setDate(new Date().getDate() + 1)
All examples return Unix timestamp, if you want to get Date
object - just wrap it with another new Date(...)
所有示例都返回 Unix 时间戳,如果你想获取Date
对象 - 只需用另一个包装它new Date(...)
回答by Wavesailor
Using some of the answers on this page and here, I came up with my own answer as none of these answers fully solved it for me.
使用本页和此处的一些答案,我想出了自己的答案,因为这些答案都没有完全解决我的问题。
Here is crux of it
这是它的关键
var startDate = "27 Apr 2017";
var numOfYears = 1;
var expireDate = new Date(startDate);
expireDate.setFullYear(expireDate.getFullYear() + numOfYears);
expireDate.setDate(expireDate.getDate() -1);
And here a a JSFiddle that has a working example: https://jsfiddle.net/wavesailor/g9a6qqq5/
这里有一个 JSFiddle,它有一个工作示例:https://jsfiddle.net/wavesailor/g9a6qqq5/
回答by Abdus Salam Azad
Use this:
用这个:
var startDate = new Date();
startDate.setFullYear(startDate.getFullYear() - 1);
回答by James Westgate
Use setFullyear as others have posted but be aware this returns a timestamp value not a date object. It is also a good candidate imho to add functionality via the prototype. This leads us to the following pattern:
使用其他人发布的 setFullyear 但请注意,这将返回时间戳值而不是日期对象。恕我直言,通过原型添加功能也是一个很好的候选者。这将我们引向以下模式:
Date.prototype.addYears = function(n) {
var now = new Date();
return new Date(now.setFullYear(now.getFullYear() + n));
};
console.log('Year from now is', new Date().addYears(1));
回答by Nadeem Haidar
In very simple way. use this code.
以非常简单的方式。使用此代码。
// define function
function nextYearDate(date1) {
var date2 = new Date(date1);
var date3 = date2.setDate(date2.getDate() - 1);
var date = new Date(date3);
var day = date.getDate();
var month = date.getMonth()+1;
var year = date.getFullYear()+1;
var newdate = year + '-' + (month < 10 ? '0' : '') + month + '-' + (day < 10 ? '0' : '') + day;
$("#next_date").val(newdate);
}
// call function.
<input type="date" name="current_date" id="current_date" value="" onblur="nextYearDate(this.value);" />
<input type="date" name="next_date" id="next_date" value="" onblur="nextYearDate(this.value);" />