Created
January 31, 2023 01:12
-
-
Save luislobo9b/026951ca9a37b74e8e23524bc5ec87c9 to your computer and use it in GitHub Desktop.
french-date-string-to-br-date.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function frenchDateStringToBrDate(date) { | |
date = date.toLowerCase().replace('é', 'e').replace('û', 'u') | |
const regexp = /(\d{2}) ([a-z]+) (\d{4})/, | |
months = [ | |
'janvier', | |
'fevrier', | |
'mars', | |
'avril', | |
'mai', | |
'juin', | |
'juillet', | |
'aout', | |
'septembre', | |
'octobre', | |
'novembre', | |
'decembre' | |
] | |
if ((regexp).test(date)) { | |
date = date.match(regexp) | |
if (months.includes(date[2])) { | |
let month = months.indexOf(date[2]) + 1 | |
if (month < 10) { | |
month = '0' + month | |
} | |
return date[1] + '/' + month + '/' + date[3] | |
} | |
} | |
return 'invalid date' | |
} | |
// example of use: | |
console.log(frenchDateStringToBrDate('dimanche, 01 janvier 2023')) | |
console.log(frenchDateStringToBrDate('lundi, 13 mars 2023')) | |
console.log(frenchDateStringToBrDate('jeudi, 29 juin 2023')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment