简体   繁体   中英

How do I display the last month with the year?

I have stored the date Nov. 1, 2020 in the variable fiscalYearStart and would like to output Oct. 2020 . Can you please show me how to do this? I have written a function, but I get the message ERROR TypeError: fiscalYearStart.getMonth is not a function

My function:

public getLastMonth () {
    this.navService.getBeginningOfFiscalYear().subscribe((yearResp: any) => {
      if (yearResp && yearResp.success) {
        const fiscalYearStart = yearResp.fiscalYearStart;
        fiscalYearStart.setMonth(fiscalYearStart.getMonth() - 1);
        const lastMonth = new Date(fiscalYearStart);
        console.log(lastMonth);
      }
    });
  }

您应该使用Date构造函数,因为fiscalYearStart它是一个字符串并且没有getMonth方法。

const fiscalYearStart = new Date(yearResp.fiscalYearStart);
public getLastMonth () {
    this.navService.getBeginningOfFiscalYear().subscribe((yearResp: any) => {
      if (yearResp && yearResp.success) {
        const fiscalYearStart = new Date(yearResp.fiscalYearStart);
        fiscalYearStart.setMonth(fiscalYearStart.getMonth() - 1);
        const lastMonth = new Date(fiscalYearStart);
        console.log(lastMonth);
      }
    });
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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