Find the current financial Quarter first date using moment.js
11,135
Solution 1
With moment 1.7.0+ it is just
moment().startOf('quarter').format('MM-DD-YYYY');
Solution 2
Getting the date in JavaScript, using moment.js to format:
var qtrDate = (function () {
var d = new Date(),
m = d.getMonth() - d.getMonth() % 3;
return moment(new Date(d.getFullYear(), m, 1)).format('MM-DD-YYYY');
}());
or
function getQuarterFirstDay (d) {
var m = d.getMonth() - d.getMonth() % 3;
return moment(new Date(d.getFullYear(), m, 1)).format('MM-DD-YYYY');
}
var d = getQuarterFirstDay(new Date());
Solution 3
A pure javascript solution:
var currentMonth=(new Date()).getMonth()
var yyyy = (new Date()).getFullYear()
var start = (Math.floor(currentMonth/3)*3)+1;
end= start+3;
startDate=new Date(start+'-01-'+ yyyy);
endDate= end>12?new Date('01-01-'+ (yyyy+1)):new Date(end+'-01-'+ (yyyy));
endDate=new Date((endDate.getTime())-1)
console.log('startDate =', startDate,'endDate =', endDate);
Author by
Alok
Updated on June 09, 2022Comments
-
Alok almost 2 years
I have read many posts here but couldn't get what I wanted. I am currently new to Javascript and don't know how to get the first
Date
of the current quarter inMM-DD-YYYY
format.Thanks in advance!