functions

This commit is contained in:
Thomas Demirdjian
2025-11-03 13:47:07 +01:00
parent 460c91f1e6
commit 7b3a47090d
16 changed files with 23868 additions and 2399 deletions
+37
View File
@@ -0,0 +1,37 @@
const normalizeDate = (input) =>
typeof input?.toDate === "function" ? input.toDate() : new Date(input);
const buildMonthKey = (timestamp) => {
const date = normalizeDate(timestamp);
const year = date.getUTCFullYear();
const month = String(date.getUTCMonth() + 1).padStart(2, "0");
return `${year}-${month}`;
};
const buildPreviousMonthContext = (referenceDate) => {
const current = referenceDate ? new Date(referenceDate) : new Date();
current.setUTCHours(0, 0, 0, 0);
current.setUTCDate(1);
const target = new Date(current);
target.setUTCMonth(target.getUTCMonth() - 1);
const year = target.getUTCFullYear();
const monthIndex = target.getUTCMonth();
const rangeStart = new Date(Date.UTC(year, monthIndex, 1, 0, 0, 0, 0));
const rangeEnd = new Date(Date.UTC(year, monthIndex + 1, 0, 23, 59, 59, 999));
const monthKey = buildMonthKey(rangeStart);
return {
year,
month: monthIndex + 1,
monthKey,
rangeStart,
rangeEnd,
};
};
module.exports = {
buildMonthKey,
buildPreviousMonthContext,
};