jvcgpt / src /utils /dates.ts
Greums's picture
major improvements to the app
a417977
raw
history blame
1.87 kB
const iso8601ToFrenchRegex = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/;
export function iso8601ToFrench(iso8601: string): string {
const matches = iso8601.match(iso8601ToFrenchRegex);
const year = matches[1];
const month = months[parseInt(matches[2], 10) - 1];
const day = matches[3];
const hours = matches[4];
const minutes = matches[5];
const seconds = matches[6];
return `${day} ${month} ${year} à ${hours}:${minutes}:${seconds}`;
}
const frenchToIso8601Regex = /(\d{1,2}) ([a-zA-Z\u00C0-\u024F]+) (\d{4}) à (\d{2}):(\d{2}):(\d{2})/;
export function frenchToIso8601(french: string): string {
const match = french.match(frenchToIso8601Regex);
if (!match) {
throw new Error('Invalid date format');
}
const [, day, month, year, hours, minutes, seconds] = match;
// Convert the month name to a numeric format
const monthNumber = (months.indexOf(month) + 1).toString();
if (!monthNumber) {
throw new Error('Invalid month name');
}
// Construct the ISO 8601 formatted string
const isoDate = `${year}-${monthNumber.padStart(2, '0')}-${day.padStart(2, '0')}T${hours}:${minutes}:${seconds}`;
return isoDate;
}
const months = [
"janvier", "février", "mars", "avril", "mai", "juin",
"juillet", "août", "septembre", "octobre", "novembre", "décembre"
];
export function getCurrentTimeIso8601(): string {
const now = new Date();
const year = now.getFullYear().toString();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`;
}