jvcgpt / src /utils /dates.ts
Greums's picture
crlf to lf
1813a37
raw
history blame
1.76 kB
const iso8601ToFrenchRegex = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/;
export function iso8601ToFrench(iso8601: string): string {
console.log("iso8601ToTokens", iso8601)
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 {
console.log("tokensToIso8601", french)
// Match the components of the date and time from the input string
// const months = {
// janvier: '01', février: '02', mars: '03', avril: '04', mai: '05',
// juin: '06', juillet: '07', août: '08', septembre: '09',
// octobre: '10', novembre: '11', décembre: '12'
// };
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"
];