[英]How to calculate the previous months in JS?
在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(",");
}
我寧願有這樣的映射。 令月= [1月,2月,3月,4月,5月]
然后得到例如“ March”-在數組月份中找到其索引,並獲取先前的索引或您想要的任何內容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.