mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-08-26 06:29:38 +00:00
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
export function toDDMMYYYY(dateString?: string | null): string | null {
|
|
if (!dateString) return null;
|
|
|
|
// Ensure dateString is valid format YYYY-MM-DD
|
|
const regex = /^\d{4}-\d{2}-\d{2}$/;
|
|
if (!regex.test(dateString)) {
|
|
throw new Error(
|
|
`Invalid date format: ${dateString}. Expected format is YYYY-MM-DD`,
|
|
);
|
|
}
|
|
|
|
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
|
|
);
|
|
});
|
|
}
|