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.