Javascript 从 Date 对象中减去天/月/年
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4121796/
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
Subtracting days/months/years from a Date object
提问by dave
var inputDate = '20/4/2010'.split('/');
var dateFormatted = new Date(parseInt(inputDate[2]), parseInt(inputDate[1]), parseInt(inputDate[0]));
var expiryDate = (dateFormatted.getDate() - 1) + '/' + dateFormatted.getMonth() + '/' + (dateFormatted.getFullYear() + year);
This is the Javascript code I'm using to work out an expiry date given a user inputted date. Currently, the expiry date is original date minus one day and original year minus X
.
这是我用来计算给定用户输入日期的到期日期的 Javascript 代码。目前,到期日是original date minus one day and original year minus X
。
The problems with this code, firstly, it doesn't take into account invalid dates. For example, if the user supplied date is '1/10/2010', the expiry date will be '0/10/2013' (assuming the expiry date is +3 years).
这段代码的问题,首先是它没有考虑无效日期。例如,如果用户提供的日期为“1/10/2010”,则到期日期将为“0/10/2013”(假设到期日期为 +3 年)。
I could do something like:
我可以做这样的事情:
var inputDate = '20/4/2010'.split('/');
var day = parseInt(inputDate[0]);
var month = parseInt(inputDate[1]);
var year = parseInt(inputDate[2]);
if (day < 1)
{
if (month == ...)
{
day = 31
month = month - 1;
}
else
{
day = 30
month = month - 1;
}
}
var dateFormatted = new Date(parseInt(inputDate[2]), parseInt(inputDate[1]), parseInt(inputDate[0]));
var expiryDate = (dateFormatted.getDate() - 1) + '/' + dateFormatted.getMonth() + '/' + (dateFormatted.getFullYear() + year);
But more problems arise... Firstly, the code gets a little convoluted. Secondly, this check would have to be done on the day. and then the month. Is there a cleaner, simpler way?
但更多的问题出现了......首先,代码变得有点复杂。其次,这项检查必须在当天进行。然后是月份。有没有更干净、更简单的方法?
Also, there's a certain circumstance that would involve me needing to calculate the expiry date to the 'end of the month' for that date. For example:
此外,在某些情况下,我需要将到期日期计算为该日期的“月底”。例如:
Expiry date is: +3 years
User date is: '14/10/2010'
Expiry date is: '31/10/2013'
I was hoping the Date object
would support these calculations but according to https://developer.mozilla.org/en/JavaScript/Reference/global_objects/date, it seems not...
我希望Date object
能支持这些计算,但根据https://developer.mozilla.org/en/JavaScript/Reference/global_objects/date,它似乎不是......
回答by Noah
Easy way to see if a date inputed is a valid date:
查看输入的日期是否为有效日期的简单方法:
var d = Date.parse('4/20/2010');
if (isNaN(d.valueOf())) {
alert ("bad date value");
}
Then, here is a dateAdd function that I use regularly. Extends the Date object, so it's easy to use:
然后,这是我经常使用的 dateAdd 函数。扩展了 Date 对象,所以它很容易使用:
Date.prototype.dateAdd = function(size,value) {
value = parseInt(value);
var incr = 0;
switch (size) {
case 'day':
incr = value * 24;
this.dateAdd('hour',incr);
break;
case 'hour':
incr = value * 60;
this.dateAdd('minute',incr);
break;
case 'week':
incr = value * 7;
this.dateAdd('day',incr);
break;
case 'minute':
incr = value * 60;
this.dateAdd('second',incr);
break;
case 'second':
incr = value * 1000;
this.dateAdd('millisecond',incr);
break;
case 'month':
value = value + this.getUTCMonth();
if (value/12>0) {
this.dateAdd('year',value/12);
value = value % 12;
}
this.setUTCMonth(value);
break;
case 'millisecond':
this.setTime(this.getTime() + value);
break;
case 'year':
this.setFullYear(this.getUTCFullYear()+value);
break;
default:
throw new Error('Invalid date increment passed');
break;
}
}
Then just use:
然后只需使用:
var d = new Date();
d.dateAdd('day', -1).dateAdd('year', 3);
T'da
达达
回答by Gunjit
A similar question has been answered here:
这里已经回答了一个类似的问题:
How to add/subtract dates with javascript?
Similar thing can be done for months and years.
类似的事情可以持续数月甚至数年。
For e.g.
例如
var date = new Date('2011','01','02');
alert('the original date is '+date);
var newdate = new Date(date);
newdate.setMonth(newdate.getMonth() - 7);
var nd = new Date(newdate);
alert('the new date is '+nd);
回答by DoXicK
var currentDate = new Date(year,month,day);
var expiryDate = new Date();
expiryDate.setTime(currentDate.getTime() + (3 * 365 * 24 * 60 * 60 * 1000));
using the number of seconds past 1970 is fine for this :-) oh, you have more rules. well after that you will still have to check for those cases...
使用 1970 年以后的秒数很好:-) 哦,你有更多的规则。在那之后,您仍然需要检查这些情况......
回答by Dr.Molle
Maybe this will be useful to you: http://code.google.com/p/datejs/wiki/APIDocumentation
也许这对你有用:http: //code.google.com/p/datejs/wiki/APIDocumentation