简体   繁体   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

Well, create an array with the months and adjust it based on the current month好吧,创建一个带有月份的数组并根据当前月份进行调整

 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);

Here is a working fiddle https://jsfiddle.net/uot8Lfdr/2/这是一个工作小提琴https://jsfiddle.net/uot8Lfdr/2/

Using the array prototype methods, concat, splice, and reverse, you can manipulate a list of months to produce the array you want.使用数组原型方法 concat、splice 和 reverse,您可以操作月份列表来生成您想要的数组。

You can get the current month as a number using new Date().getMonth()您可以使用 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);

Dynamically without an initial array using Date#toLocaleString() to retrieve month names.使用Date#toLocaleString()在没有初始数组的情况下动态检索月份名称。

This gives you access to localization automatically by accepting a locale as a parameter.这使您可以通过接受语言环境作为参数来自动访问本地化。 It should be noted that this will use the local system timezone, you'll need to adjust if you want UTC.应该注意的是,这将使用本地系统时区,如果需要 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; }

If you have control over how the months are defined, you could define them backwards to start, and avoid using reverse() , or other loops.如果您可以控制月份的定义方式,则可以向后定义它们以开始,并避免使用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.

相关问题 如何从一系列月份对象[{month:&#39;Jan&#39;id:&#39;1},{month:&#39;Feb&#39;,id:&#39;2},…{month:&#39;Dec&#39;,id :12}]? - How to get next 4 months from current month in an array of month objects [{month:'Jan' id:'1},{month:'Feb', id:'2},…{month:'Dec', id:12}]? 连续显示所有 12 个月,但从当前月份开始 - Display all 12 months serially but starting from the current month 如何在javascript中动态获取基于本月的当前3个月? - How to get Current 3 months based on this month dynamically in javascript? Javascript - 按当前月份的时间顺序对月份数组进行排序 - Javascript - Sort months array in chronological order basis current month 在 javascript 中构建从当前月份开始的前三个月的数组 - Build an array with previous three months from current month in javascript Cognos-值提示,根据年份显示从一月到当前月份的月份 - Cognos - value prompt to display months from january to current month based on year 根据当前月份动态选择日期 - 选择列表 - Dynamic selection of dates based on current month - select list 仅在月份下拉列表中需要当前月份和未来月份 - Need current month and upcoming months only in month dropdown Angularjs从1个选择框中按过去12个月的月份筛选 - Angularjs filter by month by the last 12 months from 1 select box 在 JS 中过滤数组以持续 12 个月 - Filter array to last 12 months in JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM