Reputation: 1502
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?
Upvotes: 1
Views: 1640
Reputation: 76
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"
Upvotes: 0
Reputation: 1181
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'
Upvotes: 1
Reputation: 1502
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/
Upvotes: 1
Reputation: 21
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
Upvotes: 1