繁体   English   中英

在 javascript 中构建从当前月份开始的前三个月的数组

[英]Build an array with previous three months from current month in javascript

我展示了一份报告,该报告带来了当月的三个月数据。 例如,这个月是 6 月,因此它带来了 4 月、5 月和 6 月的数据。

我想在 javascript/angularjs 中构建一个数组,它给了我从当前日期开始的前三个月,如下所示:

$scope.montharray = [
{
monthname: "All",
Monthnumber: "4,5,6"
},
{
monthname: "April",
MonthNumber:"4",
},
{
monthname: "May",
MonthNumber:"5",
},
{
monthname: "June",
MonthNumber:"6",
}
]

有人可以帮助我如何实现像这样滚动三个月的数组吗?

TIA

您将需要一个数组来查找月份名称。 用新日期计算当前月份。 使用 Array.from 创建您的数组。 如果您的 go 超过 12,请确保 % mod by 12 以返回到 1 月。

 const monthNames = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]; const currentMonth = new Date().getMonth() + 1; const monthArray = Array.from({ length: 4 }, (_, index) => index === 0? { monthname: 'All', monthNumber: `${currentMonth},${(currentMonth + 1) % 12},${(currentMonth + 2) % 12}` }: { monthName: monthNames[(currentMonth + index - 2) % 12], monthNumber: `${(currentMonth + index - 1) % 12}` } ); console.log(monthArray);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM