1

I need the date of last September with moment.js - but dynamically.

Current Date

currentDate:string = moment().format('YYYY-MM'); // This will be 2017-10

How to know when the last september was from the current date? I need somethink like this:

lastSteptember = moment(this.currentDate).getDateOfLast('september').format('YYYY-MM');

So the result from now would be:

2017-09

But 3 months ago the result would have been another:

2016-09

How do I can handle this with moment.js?

Vueer
  • 1,432
  • 3
  • 21
  • 57
  • Is [`month()`](http://momentjs.com/docs/#/get-set/month/) what you are looking for? I did not fully understand what you are trying to do. What did you try by yourself so far? – VincenzoC Oct 27 '17 at 08:46
  • `month()` gives me the current month. But I need the last september as result. I am writing an application in which one year starts in September and ends in September. Therefore to get specific data, I need to know from the current month which year was last September. – Vueer Oct 27 '17 at 08:52
  • `month(8)` and `month('September')` also works as a setter, if you want to customize moment behaviour see [Customize](http://momentjs.com/docs/#/customization/) section of the docs. – VincenzoC Oct 27 '17 at 09:11
  • In order to help readers please mark the answer for your problem. – Skoempie Oct 27 '17 at 11:47
  • I can´t mark the solution I have implemented until two days from now, because it is my own answer.. – Vueer Oct 27 '17 at 11:50

4 Answers4

1

I dont know how to make it work with moment. But you can use my function to get last month by month number:

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

Use it:

getLastMonth(11); //return 11-2016

getLastMonth(10); //return 10-2017

getLastMonth(9); //return 09-2017
Nhat Nam
  • 21
  • 2
1

I found a pretty solution and solved it like this:

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

https://momentjs.com/docs/#/query/is-same-or-after/

Vueer
  • 1,432
  • 3
  • 21
  • 57
1

This function will return the month of the previous year if the given month is lower than the current month, otherwise it will return the month with the current year.

  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'
Skoempie
  • 1,171
  • 10
  • 18
0

You can try the following :

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

In the above JavaScript we check whether the month has passed in the current moment object.

This allows us to directly check from any moment object and of any month eg:

var customDate = moment("01 December 2001");
customDate.getLast("September"); //"2001-09"
Paul K
  • 76
  • 2