Get Current and Previous Quarter JS
Gets the current and previous quarter date range based on the current date.
JavaScript
function getCurrentPreviousQuarter(value) {
var today = new Date(),
quarter = Math.floor((today.getMonth() / 3)),
startDate,
endDate;
switch (value) {
case "previous":
startDate = new Date(today.getFullYear(), quarter * 3 - 3, 1);
endDate = new Date(startDate.getFullYear(), startDate.getMonth() + 3, 0);
break;
default:
startDate = new Date(today.getFullYear(), quarter * 3, 1);
endDate = new Date(startDate.getFullYear(), startDate.getMonth() + 3, 0);
break;
}
return {
StartDate: startDate,
EndDate: endDate
};
}
document.write(getCurrentPreviousQuarter("current").StartDate);
document.write(getCurrentPreviousQuarter("previous").EndDate);