6

我有一个像这样的剑道 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 个月。

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

谢谢大家!

4

5 回答 5

16

包括月份的年份

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);
}

于 2018-11-12T15:01:27.277 回答
9

我还需要一份过去 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;
    }
}
于 2015-02-25T09:46:50.940 回答
4

使用 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;
};
于 2013-09-26T06:45:22.923 回答
1

最近 N(24) 个月增量 - 显示时间序列数据很重要

var monthName = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec");
        var months = [];
        var d = new Date();
        d.setDate(1);
        for (i=0; i<=24; i++) {
//                console.log(monthName[d.getMonth()] + ' ' + d.getFullYear());
            months.push(monthName[d.getMonth()] + ' ' + d.getFullYear());
            d.setMonth(d.getMonth() - 1);
        }
        months = months.reverse();
        for (i=0; i<=24; i++) {
          console.log(months[i]); 
        }

于 2021-01-12T09:33:15.393 回答
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"]
于 2019-12-22T07:27:26.420 回答