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...
convertToMomentDates
in this function? If you don't want the timezone to change, you can remove the ".tz" function .