简体   繁体   English

连续显示所有 12 个月,但从当前月份开始

[英]Display all 12 months serially but starting from the current month

I am working on a requirement where I have 12 ion slides and the names of the slides are month names starting from the current month.我正在处理一个要求,我有 12 张离子幻灯片,幻灯片的名称是从当前月份开始的月份名称。 For ex: If the current month is June, then the name of the first slide should start from June 2020 and go on till May 2021. Well I tried it but not able to achieve.例如:如果当前月份是 6 月,那么第一张幻灯片的名称应该从 2020 年 6 月开始,go 到 2021 年 5 月。好吧,我尝试过但无法实现。 Even if I handle the month, then I am not able to dynamically change the year after December.即使我处理月份,我也无法在 12 月之后动态更改年份。 Here is my code:这是我的代码:

my html file我的 html 文件

  <ion-toolbar class="toolbars" #ionDragFooter>    
          <ion-title class="ion-text-center">{{currValue}}</ion-title>
  </ion-toolbar>

My.ts file我的.ts 文件

ngOnInit() {
 swipeNext()
}

 var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"];

  var d = new Date();


if(index == 1){
        var curMn=monthNames[d.getMonth()]+ d.getFullYear();
        console.log(curMn)
        this.currValue=curMn;
      }
      else if(index == 2){
        var curMn=monthNames[d.getMonth()+1]+ d.getFullYear();
        this.currValue=curMn;
      }
      else if(index == 3){
        var curMn=monthNames[d.getMonth()+2]+ d.getFullYear();
        this.currValue=curMn;
}

and so on for all other month.以此类推其他所有月份。 I need to completely automate the month and year starting from the current month.我需要从当前月份开始完全自动化月份和年份。 May I know where I went wrong?我可以知道我哪里出错了吗?

You can create a date, then increment the month by 1 eleven times to get 12 months of dates.您可以创建一个日期,然后将月份增加 111 次以获得 12 个月的日期。 The month name can be found using toLocaleString with appropriate options. The month name can be found using toLocaleString with appropriate options.

You need to be careful when incrementing months, eg增加月份时需要小心,例如

let d = new Date(2020,0,31); // 31 Jan 2020
d.setMonth(d.getMonth() + 1) // 31 Feb 2020 -> 2 Mar 2020
d.toDateString();            // Mon Mar 02 2020

So best to set the date to the 1st of the month initially:所以最好最初将日期设置为每月 1 日:

 // Optionally provide a date for the starting month function getMonthNames(date = new Date()) { // Copy input date so don't modify it let d = new Date(+date); // Set to 1st of month d.setDate(1); let result = []; // Get 12 month names, starting with current month for (let i=0; i<12; i++) { result.push(d.toLocaleString('default',{month:'long'})); d.setMonth(d.getMonth() + 1); } return result; } console.log(getMonthNames());

To properly change the date you have to increase the date by the amount of months in the date object (in your case d ):要正确更改日期,您必须将日期增加日期 object 中的月数(在您的情况下为d ):

 var d = new Date(); //REM: This will print out the next month, yet d still is unchanged console.log(d.getMonth()+1); console.log(d); console.log('Month did not increase: ', d.getMonth()); //REM: You have to increase your date by one month in the date object d.setMonth(d.getMonth() + 1); console.log(d); console.log('Month did increase: ', d.getMonth()); //REM: By which it will change the year for you, once it is time for it. d.setMonth(d.getMonth() + 11); //REM: +1 from before console.log(d); console.log('Year did increase: ', d.getFullYear());

In your case that would be:在你的情况下,这将是:

 var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; var d = new Date(); d.setDate(1); //REM: To prevent month skipping. for(var i=0; i<12; i++){ d.setMonth(d.getMonth() + 1); console.log(monthNames[d.getMonth()], d.getFullYear()) };

Some further reading on the issue 关于这个问题的一些进一步阅读

This is what I came up with:这就是我想出的:

const monthNames = ["January", "February", "March", "April", "May", "June",
     "July", "August", "September", "October", "November", "December"];
   
    const today = new Date();
    //gives you year
    const year = today.getFullYear();
    //gives you month as number/index;
    const month = today.getMonth();

    //stores sequenced month names
    const sequencedNames = [];

    //creates June 2020 through December 2020
    for(let i = month; i < monthNames.length; i++) {
        let monthName = monthNames[i];
        sequencedNames.push(monthName + " " + year)
    }
    
    //creates January 2021 through May 2021
    for(let i = 0; i < month; i++) {
        let monthName = monthNames[i];
        sequencedNames.push(monthName + " " + (year + 1))
    }

    console.log(sequencedNames)

Essentially 2 for loops, the first iterating from the current month to the end of the year, and then the second from the start of the year to the current month -- though adding 1 year.本质上是 2 个 for 循环,第一个从当前月份迭代到年底,然后第二个从年初迭代到当前月份——尽管增加了 1 年。

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

相关问题 基于当月的 12 个月动态数组 - 12 months dynamic array based on current month 如何从一系列月份对象[{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}]? 从月份下拉选项中显示当前月份的 DIV - Display DIV for current month from months dropdown options 隐藏前几个月从当月开始 - hide all previous months start from the current month 如何使用moment.js获取从当前月份到过去一个月的所有月份列表 - How to get a list of all months from current month to a month in the past using moment.js Cognos-值提示,根据年份显示从一月到当前月份的月份 - Cognos - value prompt to display months from january to current month based on year 获取从提供的 ISO 日期到当前月份和年份的所有月份和年份 - Get all the months and year from provided ISO Date to current month and year Angularjs从1个选择框中按过去12个月的月份筛选 - Angularjs filter by month by the last 12 months from 1 select box 如何从当前日期开始获取过去的12个月 - How to get Past 12months with year from current date 从当前月份开始的月份中打印3年日历 - Printing 3 year calendar in months staring from the current month
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM