43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
/**
|
|
* Utilitaires pour la gestion des dates
|
|
* Gère correctement les conversions entre heure locale et UTC
|
|
*/
|
|
|
|
/**
|
|
* Convertit une chaîne datetime-local (format: "2025-01-15T14:30") en chaîne MySQL
|
|
* Format de sortie: "YYYY-MM-DD HH:mm:ss"
|
|
* Préserve l'heure locale sans aucune conversion UTC
|
|
*/
|
|
export function parseLocalDateTime(dateTimeString: string): string {
|
|
if (!dateTimeString) {
|
|
throw new Error('Date string is required');
|
|
}
|
|
|
|
// Le format datetime-local est "YYYY-MM-DDTHH:mm"
|
|
const match = dateTimeString.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/);
|
|
|
|
if (!match) {
|
|
throw new Error(`Invalid date format: ${dateTimeString}. Expected YYYY-MM-DDTHH:mm`);
|
|
}
|
|
|
|
const [, year, month, day, hours, minutes] = match;
|
|
|
|
// Retourner directement au format MySQL sans passer par un objet Date
|
|
// Cela évite toute conversion de fuseau horaire
|
|
return `${year}-${month}-${day} ${hours}:${minutes}:00`;
|
|
}
|
|
|
|
/**
|
|
* Formate une Date en chaîne datetime-local pour les inputs HTML
|
|
* Format: "YYYY-MM-DDTHH:mm"
|
|
*/
|
|
export function formatToLocalDateTime(date: Date): string {
|
|
const year = date.getFullYear();
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
const hours = String(date.getHours()).padStart(2, '0');
|
|
const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
|
|
return `${year}-${month}-${day}T${hours}:${minutes}`;
|
|
}
|