最近 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);
相关文章:
- Javascript最近的p标记文本未选择
- Javascript数组只保存最近的对象
- 使用JavaScript查找数组中最近的日期
- 如何在Javascript中获取最近的实体
- 如何使用javascript显示离我所在位置最近的纬度和经度
- 算法导航:如何查找最近的日期(javascript)
- 如何使用谷歌地图自动计算和显示机场的最近距离?使用jquery或javascript
- 如何使用jQuery或JavaScript从用户位置返回3个最近的位置
- jQuery/Javascript:如何使用Javascript或jQuery清除文本框上的最近搜索
- 如何将最近几个月计算为一个时期,并通过javascript更新到图表
- 最近 12 个月在 Javascript 中
- 创建一个从今天开始的最近 7 天的名称数组 - javascript
- 如何在javascript中筛选日期以仅获取最近4个月的日期
- 如何使用javascript从最近的li元素向pre-tag添加id
- javascript数组查找具有当前日期+日期的最近日期
- 使用javascript查找元素相对于最近相对定位的父元素的位置
- 仅在javascript日期在最近10天内的html页面上显示数据
- 在javascript中获取最近的笛卡尔轴对齐矢量
- 获取img的宽度和高度并将其添加到其中'与javascript最近的父级
- 谷歌街景API JavaScript最近的餐厅