繁体   English   中英

基于当月的 12 个月动态数组

[英]12 months dynamic array based on current month

我正在尝试基于当前月份构建一个 12 个月的数组,因此当前月份是三月我的数组应该如下所示:

const months =[“March”, “February”, “January”, “December”, “November”, “October”] etc, all the way to 12 previous months

好吧,创建一个带有月份的数组并根据当前月份进行调整

 const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], month = (new Date()).getMonth(), previous = months.splice(0, month); // if you have to use this in a front-end app, you might want to use .concat(...) instead of the spread operator const output = [...previous.reverse(), ...months.reverse()] console.log(output)

 const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']; let curMonth = 'Mar', ctr = 0, ref = months.indexOf(curMonth), sorted = []; while (ctr <= 11) { let n = ref - ctr n = n >= 0 ? n : 11 + n; let m = months[n] sorted.push(m) ctr++; } console.log(sorted)

const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const currentMonth = "March";

// First find current month index
const currentMonthIndex = months.findIndex((item) => currentMonth === item)

// Then reorder the array
const orderedMonths = months.slice(currentMonthIndex).concat(months.slice(0, currentMonthIndex));
console.log('orderedMonths', orderedMonths);

这是一个工作小提琴https://jsfiddle.net/uot8Lfdr/2/

使用数组原型方法 concat、splice 和 reverse,您可以操作月份列表来生成您想要的数组。

您可以使用 new Date().getMonth() 以数字形式获取当前月份

 const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; const offsetReverseMonths = months.concat(...months.splice(0, new Date().getMonth())).reverse(); console.log(offsetReverseMonths);

使用Date#toLocaleString()在没有初始数组的情况下动态检索月份名称。

这使您可以通过接受语言环境作为参数来自动访问本地化。 应该注意的是,这将使用本地系统时区,如果需要 UTC,则需要进行调整。

 function getPriorTwelveMonths({ date = new Date, locale = 'default' } = {}) { const monthName = (date) => date.toLocaleString(locale, { month: 'long' }); let month = date.getMonth(), result = []; for (let i = 0; i < 12; i++) { date.setMonth(((month - i) + 12) % 12); result.push(monthName(date)); } return result; } const defaults = getPriorTwelveMonths(), // pass a locale korean = getPriorTwelveMonths({ locale: 'ko-KR' }), // or a date and locale frenchDec1995 = getPriorTwelveMonths({ date: new Date('1995-12-17T03:24:00'), locale: 'fr' }); console.log(`[${defaults.join(',')}]`); console.log(`[${korean.join(',')}]`); console.log(`[${frenchDec1995.join(',')}]`);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

如果您可以控制月份的定义方式,则可以向后定义它们以开始,并避免使用reverse()或其他循环。

const months = ["December", "November", "October", "September", "August", "July", "June", "May", "April", "March", "February", "January"];
const monthSpot = 12 - new Date().getMonth() - 1;
const result = [...months.splice(monthSpot, 12), ...months];
console.log(result);

暂无
暂无

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

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