簡體   English   中英

連續顯示所有 12 個月,但從當前月份開始

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

我正在處理一個要求,我有 12 張離子幻燈片,幻燈片的名稱是從當前月份開始的月份名稱。 例如:如果當前月份是 6 月,那么第一張幻燈片的名稱應該從 2020 年 6 月開始,go 到 2021 年 5 月。好吧,我嘗試過但無法實現。 即使我處理月份,我也無法在 12 月之后動態更改年份。 這是我的代碼:

我的 html 文件

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

我的.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;
}

以此類推其他所有月份。 我需要從當前月份開始完全自動化月份和年份。 我可以知道我哪里出錯了嗎?

您可以創建一個日期,然后將月份增加 111 次以獲得 12 個月的日期。 The month name can be found using toLocaleString with appropriate options.

增加月份時需要小心,例如

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

所以最好最初將日期設置為每月 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());

要正確更改日期,您必須將日期增加日期 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());

在你的情況下,這將是:

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

關於這個問題的一些進一步閱讀

這就是我想出的:

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)

本質上是 2 個 for 循環,第一個從當前月份迭代到年底,然后第二個從年初迭代到當前月份——盡管增加了 1 年。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM