Javascript 如何使用moment.js动态获取去年9月的日期

Javascript 如何使用moment.js动态获取去年9月的日期,javascript,angular,momentjs,ionic3,angular2-moment,Javascript,Angular,Momentjs,Ionic3,Angular2 Moment,我需要在moment.js中显示去年9月的日期-但动态显示 当前日期 currentDate:string=moment().format('YYYY-MM');//这将是2017-10年 如何知道最后一个9月是从当前日期算起的?我需要这样的想法: lastSteptember=moment(this.currentDate).getDateOfLast('九月').format('YYYY-MM') 因此,从现在开始的结果将是: currentYear: number = moment().y

我需要在moment.js中显示去年9月的日期-但动态显示

当前日期

currentDate:string=moment().format('YYYY-MM');//这将是2017-10年

如何知道最后一个9月是从当前日期算起的?我需要这样的想法:

lastSteptember=moment(this.currentDate).getDateOfLast('九月').format('YYYY-MM')

因此,从现在开始的结果将是:

currentYear: number = moment().year();
lastYear: number = moment().subtract(1, 'years').year();
nextYear: number = moment().add(1, 'years').year();

if(moment().isSameOrAfter(this.currentYear + '-09-01', 'month')) {
  this.currentServiceYearStartDate = this.currentYear + '-09-01';
  this.currentServiceYearEndDate = this.nextYear + '-08-31';
} else {
  this.currentServiceYearStartDate = this.lastYear + '-09-01';
  this.currentServiceYearEndDate = this.currentYear + '-08-31';
}

console.log('Startdate: ' + this.currentServiceYearStartDate);
console.log('Enddate: ' + this.currentServiceYearEndDate);
2017-09

但3个月前结果会是另一个:

currentYear: number = moment().year();
lastYear: number = moment().subtract(1, 'years').year();
nextYear: number = moment().add(1, 'years').year();

if(moment().isSameOrAfter(this.currentYear + '-09-01', 'month')) {
  this.currentServiceYearStartDate = this.currentYear + '-09-01';
  this.currentServiceYearEndDate = this.nextYear + '-08-31';
} else {
  this.currentServiceYearStartDate = this.lastYear + '-09-01';
  this.currentServiceYearEndDate = this.currentYear + '-08-31';
}

console.log('Startdate: ' + this.currentServiceYearStartDate);
console.log('Enddate: ' + this.currentServiceYearEndDate);
2016-09


我如何使用moment.js处理这个问题?

我不知道如何使用moment。但您可以使用我的函数按月份获取上个月的编号:

var getLastMonth = function(month){
        if(month<1 || month>12) return undefined;
        let year = (month<=moment().month())? (moment().year()) : (moment().year()-1);
        return moment(month + "-" + year, "MM-YYYY");
}

我找到了一个很好的解决方案,并这样解决:

currentYear: number = moment().year();
lastYear: number = moment().subtract(1, 'years').year();
nextYear: number = moment().add(1, 'years').year();

if(moment().isSameOrAfter(this.currentYear + '-09-01', 'month')) {
  this.currentServiceYearStartDate = this.currentYear + '-09-01';
  this.currentServiceYearEndDate = this.nextYear + '-08-31';
} else {
  this.currentServiceYearStartDate = this.lastYear + '-09-01';
  this.currentServiceYearEndDate = this.currentYear + '-08-31';
}

console.log('Startdate: ' + this.currentServiceYearStartDate);
console.log('Enddate: ' + this.currentServiceYearEndDate);

如果给定月份低于当前月份,此函数将返回上一年的月份,否则将返回当前年份的月份

  function getMonthWithPresentOrPastYear(month, format) {
      currentMonth = parseInt(moment().format('M'));
      givenMonth = parseInt(moment().month(month).format('M'));
      format = format || 'YYYY-MM';

      return (givenMonth >= currentMonth) ?
        moment().month(month).format(format) : //  Present
        moment().month(month).subtract(1, 'year').format(format); // Past
    };

    getMonthWithPresentOrPastYear('January'); // returns '2016-01'
    getMonthWithPresentOrPastYear('September'); // returns '2016-09'
    getMonthWithPresentOrPastYear('October'); // returns '2017-10'
    getMonthWithPresentOrPastYear('December', 'YYYY/DD'); // returns '2017/12'

您可以尝试以下操作:

Object.getPrototypeOf(moment()).getLast = function(month,format="YYYY-MM"){
 var date;
 if(this.diff("01 "+month+" "+this.year()) >= 0 || this.format("MMMM").toLowerCase() === month.toLowerCase)
    date = moment(this).month(month);
  else
    date = moment(this).month(month).subtract(1,"years");
 return date.format(format);
});
在上面的JavaScript中,我们检查当前时刻对象中的月份是否已经过去

这使我们能够从任何时刻和任何月份直接检查对象,例如:

var customDate = moment("01 December 2001");
customDate.getLast("September"); //"2001-09"

你在找什么?我不完全明白你想做什么。到目前为止,您自己尝试了什么?
month()
提供了当前月份。但我需要去年九月的结果。我正在写一份申请书,其中一年从9月开始,到9月结束。因此,为了获得具体的数据,我需要从当前月份知道去年9月是哪一年。
month(8)
month(“九月”)
也可以作为设置器,如果您想自定义即时行为,请参阅文档的一节。为了帮助读者,请标记您问题的答案。我无法标记我在两天后实施的解决方案,因为这是我自己的答案。。