简体   繁体   English

如何显示年份的最后一个月?

[英]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 .我已将Nov. 1, 2020的日期存储在变量fiscalYearStart并希望输出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我已经编写了一个函数,但我收到消息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);
      }
    });
  }

暂无
暂无

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

相关问题 我如何使用日期js将日期格式更改为仅显示月份和年份 - How do i change date format to display month and year only using moment js 如何在日期选择器中显示月份和年份 - How to display on month and year in datepicker 每当我在 jquery ui datepicker 中更改月/年时,如何获取该月的第一个日期和最后一个日期 - how to get the first date and last date of the month whenever I change the month/year in jquery ui datepicker 如何转换此日期范围(月,年)日期选择器以数字格式显示月份的输出 - How do I convert this date range (month,year) date picker to show the output of the month in numeric format 使用D3.js从JSON显示上周,上个月和上一年数据的条形图 - Display Bar Chart for Last week, Last Month, and Last Year Data from JSON using D3.js 如何在jQuery datepicker中获取所选月份的最后一天? - How do I get the last day of the selected month in jQuery datepicker? 如何在 x 轴上显示月份和年份,并在 Highcharts 中显示为数字? - How can I have month and year in x axis, display as number in Highcharts? 如何在jsp中显示选定年份(年份)和月份(月份)中的所有日历日期? - How can I display all the calendar dates in jsp, for a selected year in years dropdown and selected month in months dropdown? 如何仅显示时间部分的月、日和年(例如 2020 年 6 月 13 日) - How can I display only month, day and year for time section(example Jun 13, 2020) 如何在javascript中从月份和年份中获取第一个日期和最后一个日期 - how to get first date and last date from month and year in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM