How to get day (number), month (number) and year from a date using month's french name?

A/B

Joined
Jan 29, 2023
Messages
7
Reaction score
1
I need to get day (number), month (number) and year from a date using month's french name.

I know how to do it using month's english name:
const dt = new Date("12 may 1996"); const day = dt.getDate(); const month = dt.getMonth(); const year = dt.getFullYear(); console.log(day, month, year); // 12 4 1996
How can I accomplish this using french for the month? I.e:
const dt = new Date("12 mai 1996");
Here is what I tried:
const dt = new Date("12 mai 1996"); const day = dt.getDate(); const month = dt.getMonth(); const year = dt.getFullYear(); console.log(day, month, year); // NaN NaN NaN
Thanks in advance.
 
Joined
Nov 13, 2020
Messages
302
Reaction score
38
<script>
const formatter = new Intl.DateTimeFormat('fr', { month: 'short' });
const month1 = formatter.format(new Date());
const month2 = formatter.format(new Date(2003, 5, 12));
alert(month1);
alert(month2);
</script>
 

A/B

Joined
Jan 29, 2023
Messages
7
Reaction score
1
<script>
const formatter = new Intl.DateTimeFormat('fr', { month: 'short' });
const month1 = formatter.format(new Date());
const month2 = formatter.format(new Date(2003, 5, 12));
alert(month1);
alert(month2);
</script>
I need to get day (number), month (number) and year from "12 mai 1996" (or any similar string where the month is written in French language).
 
Joined
Mar 5, 2023
Messages
36
Reaction score
12
To parse a date string with a French month name, you can use the Intl.DateTimeFormat constructor with the fr locale to create a formatter for French dates, and then use the Date.parse() method to convert the formatted string to a date object.

Here is an example code snippet:

JavaScript:
const dtString = "12 mai 1996";
const formatter = new Intl.DateTimeFormat("fr", { month: "long" });
const dt = new Date(Date.parse(dtString.replace(/(\d+)\s(\w+)\s(\d+)/, "$1 " + formatter.formatToParts(new Date("$2 1 2000"))[0].value + " $3")));
const day = dt.getDate();
const month = dt.getMonth();
const year = dt.getFullYear();
console.log(day, month, year); // 12 4 1996

In this code, we first define the date string as dtString. Then, we create a DateTimeFormat object for the fr (French) locale, with the month option set to "long" to get the full name of the month. We use this formatter to convert the month name in the date string to its numeric equivalent by creating a new Date object with a day value of 1 and a month value of the full French month name, then getting the value of the first part of the formatted date (which will be the month number).

Next, we use a regular expression to replace the French month name in the date string with the month number obtained from the formatter, and then pass the resulting string to the Date.parse() method to get a date object.

Finally, we extract the day, month, and year values from the date object using the getDate(), getMonth(), and getFullYear() methods, respectively.

Note that this code assumes that the input date string has the format "day monthName year" (with a space between each component), and that the month name is in French. If the format or language is different, you may need to modify the regular expression or formatter options accordingly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,795
Latest member
YolandaCar

Latest Threads

Top