我有一个剑道UI图像这一个,并有显示过去12个月的轴线今天的日期。
我发现这可以将日期对象扩展到上个月。问题似乎是当我收到“ 2013/05/31”之类的日期而前几个月没有第31天时。
Date.prototype.toPrevMonth = function (num) {
var thisMonth = this.getMonth();
this.setMonth(thisMonth-1);
if(this.getMonth() != thisMonth-1 && (this.getMonth() != 11 || (thisMonth == 11 && this.getDate() == 1)))
this.setDate(0);
}
new Date().toPrevMonth(11),
new Date().toPrevMonth(10),
new Date().toPrevMonth(9),
new Date().toPrevMonth(8),
new Date().toPrevMonth(7),
new Date().toPrevMonth(6),
new Date().toPrevMonth(5),
new Date().toPrevMonth(4),
new Date().toPrevMonth(3),
new Date().toPrevMonth(2),
new Date().toPrevMonth(1),
new Date().toPrevMonth(0)
谁能帮助我解决if状态?
该函数仅显示前一个月,但是我需要前12个月。
还是有更简单的解决方案?:-)
谢谢大家!
使用Datejs(http://www.datejs.com/)
它具有一个内置功能,可以增加几个月的时间:
Date.today().addMonths(-6);
更新:由于您不能包含外部文件,因此以下是Datejs中的相关方法。
/*
* Copyright (c) 2006-2007, Coolite Inc. (http://www.coolite.com/). All rights reserved.
* License: Licensed under The MIT License. See license.txt and http://www.datejs.com/license/.
* Website: http://www.datejs.com/ or http://www.coolite.com/datejs/
*/
Date.isLeapYear = function (year) {
return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
};
Date.prototype.isLeapYear = function () {
var y = this.getFullYear();
return (((y % 4 === 0) && (y % 100 !== 0)) || (y % 400 === 0));
};
Date.getDaysInMonth = function (year, month) {
return [31, (Date.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
};
Date.prototype.getDaysInMonth = function () {
return Date.getDaysInMonth(this.getFullYear(), this.getMonth());
};
Date.prototype.addMonths = function (value) {
var n = this.getDate();
this.setDate(1);
this.setMonth(this.getMonth() + value);
this.setDate(Math.min(n, this.getDaysInMonth()));
return this;
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句