I have a function in Javascript that returns the date ranges of the current/previous/next quarter. For example, for current quarter it would return 2019-01-01
and 2019-03-31
. For some reason, a few colleagues have said that the date ranges are inaccurate for them: for them it returns 2018-12-31
and 2019-02-27
. I noticed that both of these users are in Germany/Poland region.
Here is my jsFiddle
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getUTCMonth() + 1),
day = '' + d.getUTCDate(),
year = d.getUTCFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
function getDate_FQ(range){
var now = new Date();
switch(range){
case 'Previous FQ':
now.setMonth(now.getMonth()-3);
break;
case 'Next FQ':
now.setMonth(now.getMonth()+3);
break;
case 'Current FQ':
break;
}
var quarter = Math.floor((now.getUTCMonth() / 3));
var firstDate = new Date(now.getUTCFullYear(), quarter * 3, 1);
var endDate = new Date(firstDate.getUTCFullYear(), firstDate.getUTCMonth() + 3, 0);
return([firstDate, endDate])
}
let [first, end] = getDate_FQ('Current FQ')
console.log(formatDate(first), formatDate(end))
How is it that one date is off by 1 day and the other is off by 1 month and 1 day?