过去12个月的Javascript

时间:2013-09-26 06:40:18

标签: javascript date kendo-ui

我得到了一张像this一样的剑道ui图表,并且必须在轴上显示今天最后12个月的日期。
我发现this扩展了日期对象以获得上个月。问题似乎是当我得到像“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个月。

或者是否有更简单的解决方案? : - )

谢谢大家!

4 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:3)

使用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;
};

答案 2 :(得分:1)

包括月份的年份

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

答案 3 :(得分:0)

我这样处理,以便按顺序输出一个新数组lastMonths,其中包含从今天开始的最近12个月:

var date = new Date();
var lastMonths = [],
    monthNames = ['Dec', 'Nov', 'Oct', 'Sep', 'Aug', 'Jul', 'Jun', 'May', 'Apr', 'Mar', 'Feb', 'Jan'];
for (var i = 0; i < 12; i++) {
    lastMonths.push(monthNames[date.getMonth()]);
    date.setMonth(date.getMonth() - 1);
}

console.log(lastMonths);

// ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]