gpt4 book ai didi

javascript - JS如何计算前几个月?

转载 作者:行者123 更新时间:2023-12-03 01:38:54 25 4
gpt4 key购买 nike

在 JavaScript 中,

如何以最快的方式计算从本月(一年)开始的所有月份?

比如说,对于输入:Jun 应该预期 Jun,May,Apr,...Jan,Dec...Jun

最佳答案

您可以遵循这样的方法:

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

function getPreviousMonths(currentMonth) {
let index = MONTHS.indexOf(currentMonth);
let result = [];

for(let j = 0; j < MONTHS.length; j++) {
let access = index - j;
if(access < 0) {
access += MONTHS.length;
}
result.push(MONTHS[access]);
}

return result.join(",");
}

关于javascript - JS如何计算前几个月?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50948479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com