过去12个月的Javascript

chr*_*ris 4 javascript date kendo-ui

我得到了像这样的剑道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)
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我解决if状态吗?
该功能仅用于显示前一个月,但我需要前12个月.

还是有一个更简单的解决方案?:-)

谢谢大家!

小智 13

包括月份的年份

var monthName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var d = new Date();
d.setDate(1);
for (i=0; i<=11; i++) {
    console.log(monthName[d.getMonth()] + ' ' + d.getFullYear());
    d.setMonth(d.getMonth() - 1);
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果您当前的日期是三月的 29.,则此方法将返回两次三月,因为 29/03/19 减去一个月是 29/02/19,不存在,因此返回相应的三月日期相反(1/03)。所以重要的是你从 d.setDate(1) 开始以避免这个问题。 (5认同)

ela*_*ver 6

我还需要过去12个月的清单,这就是我所做的:

var theMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var today = new Date();
var aMonth = today.getMonth();
var i;
for (i=0; i<12; i++) {
    document.writeln(theMonths[aMonth] + '<br>'); //here you can do whatever you want...
    aMonth++;
    if (aMonth > 11) {
        aMonth = 0;
    }
}
Run Code Online (Sandbox Code Playgroud)


cbe*_*ner 5

使用 Datejs ( http://www.datejs.com/ )

它有一个内置函数来添加月份:

Date.today().addMonths(-6);
Run Code Online (Sandbox Code Playgroud)

更新: 由于您无法包含外部文件,因此以下是 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;
};
Run Code Online (Sandbox Code Playgroud)