最近 12 个月在 Javascript 中

Last 12 months in Javascript

本文关键字:Javascript 最近      更新时间:2023-09-26

我得到了像这样的剑道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 个月。

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

谢谢大家!

包括月份的年份

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

我还需要过去 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;
    }
}

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

最近 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]); 
        }

我这样处理它,按顺序输出一个新的数组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"]

我需要一个函数来处理上个月的各种数量以及排除当前月份的能力

function getLastMonths(monthCount, excludeCurrentMonth) {
  //debugger;  
  var currentDate = new Date(); // Get the current date
  var months = []; // Array to store the last N months
  // if the current month should be excluded, off by default
  if(excludeCurrentMonth) {
      currentDate.setMonth(currentDate.getMonth() - 1);
  }
    
  for (var i = 0; i < monthCount; i++) {
    var currentMonth = currentDate.getMonth(); // Get the current month (0-11)
    var currentYear = currentDate.getFullYear(); // Get the current year
    // Add the current month and year to the array
    months.unshift(currentYear + "-" + (currentMonth + 1) );
    // Move to the previous month
    currentDate.setMonth(currentDate.getMonth() - 1);
  }
  return months;
}
// Usage
var last12Months = getLastMonths(12);
console.log(last12Months);
var last12MonthsWithoutCurrentOne = getLastMonths(12, true);
console.log(last12MonthsWithoutCurrentOne);