Implement month-based date formatting and filtering in PowerClamp service

This commit is contained in:
faris Aljohari
2025-05-07 12:17:17 +03:00
parent d40fb7a762
commit 91abfb41ab
4 changed files with 53 additions and 21 deletions

View File

@ -12,3 +12,27 @@ export function toDDMMYYYY(dateString?: string | null): string | null {
const [year, month, day] = dateString.split('-');
return `${day}-${month}-${year}`;
}
export function toMMYYYY(dateString?: string | null): string | null {
if (!dateString) return null;
// Ensure dateString is valid format YYYY-MM
const regex = /^\d{4}-\d{2}$/;
if (!regex.test(dateString)) {
throw new Error(
`Invalid date format: ${dateString}. Expected format is YYYY-MM`,
);
}
const [year, month] = dateString.split('-');
return `${month}-${year}`;
}
export function filterByMonth(data: any[], monthDate: string) {
const [year, month] = monthDate.split('-').map(Number);
return data.filter((item) => {
const itemDate = new Date(item.date);
return (
itemDate.getUTCFullYear() === year && itemDate.getUTCMonth() + 1 === month
);
});
}