I'm using the followiing approach to get the end of a quarter:
function convertToMomentDates(dates) {
return dates.map((date) => moment(date).tz("Europe/Rome"));
}
function getEndOfQuarter(dates) {
const lastDate = dates.pop();
if (!lastDate || !lastDate.isValid()) return null;
console.log(lastDate.endOf("quarter").format());
return lastDate.endOf("quarter").format();
}
const dates = [
"2018-10-01T01:00:00+01:00",
"2019-01-01T01:00:00+01:00",
];
const endOfQuarterResult = getEndOfQuarter(convertToMomentDates(dates));
console.log(
endOfQuarterResult ===
"2019-03-31T23:59:59+01:00"
);
document.getElementById('result').innerHTML = endOfQuarterResult;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.14/moment-timezone-with-data.min.js"></script>
<div id="result"></div>
I mean I'm using all my dates with +1:00 offset and it gets changed to +2:00 after the endOf function, what would be the correct implementation in order to not change the offset...
On this case the expected result is ok due accoridng Italy/Rome timezone at the end of march, in this case the end of the last quarter it the offset changes to +2. more infor could be seen on this url: https://www.timeanddate.com/time/zone/italy/rome.