Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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");


Thanks in advance.

What I have 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
Posted
Updated 5-Feb-23 10:52am
Comments
Richard MacCutchan 5-Feb-23 7:33am    
You need to ensure that the correct locale is set on the system running the code.
Peter_in_2780 5-Feb-23 7:39am    
I suggest you investigate javascript LOCALEs, but I'm not sure how consistent the various browsers are in their support.
LB2371 5-Feb-23 13:27pm    
That's the issue: how to set the Locale in order to use months' names in French language?

1 solution

As others have mentioned, you need to work with a LOCALE or CULTURE. Here are 2 Google Searches that have a lot of information and answers on this subject:

1. javascript locale date - Google Search[^] which give answers like: javascript Date.prototype.toLocaleDateString() - CodeProject Reference[^]

2. javascript culture - Google Search[^] which gives answers like: How to detect current culture in javascript - David Guida[^]

UPDATE

You really need to google to find answers and not demand solutions be done for you. We are volunteers, not paid coders to do free work for you.

Again, research is key. I have done this for you: js date parse french - Google Search[^] which gives me this solution: Parse a date string in a specific locale (not timezone!)[^] which uses the lib: Moment.js | Home[^].

or this search: js convery french month name to english date - Google Search[^] which gives two solutions: How To Convert A Foreign Month In A Date String Into English[^] which suggests 2 different libs: GitHub - globalizejs/globalize: A JavaScript library for internationalization and localization that leverages the official Unicode CLDR JSON data[^] and Siltaar / month_nb · GitLab[^] and GitHub - datejs/Datejs: A JavaScript Date and Time Library[^] - the second lib looks most promising...

You can do it manually:
JavaScript
let input = "12 mai 1996";

let months = [
  "janvier",
  "février",
  "mars",
  "avril",
  "mai",
  "juin",
  "juillet",
  "août",
  "septembre",
  "octobre",
  "novembre",
  "décembre"
];

let [day, month, year] = input.split(" ");
month = months.indexOf(month) + 1;
let date = new Date(`${month}/${day}/${year}`);
let output = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();

console.log(output);  // Output: "5/12/1996"

... but then you will need to manually do it for every language that you want to support.

As I said, research is key when you don't know how, or use brute force, just like I have in my code. This is what I have done for you to answer your question.
 
Share this answer
 
v2
Comments
LB2371 5-Feb-23 17:15pm    
Maybe my question is not clear (I'm new to JS dates).
I'm writing my own script, and - as I wrote - I need to get day (number), month (number) and year from a date using month's French name, in order to manipulate them in a specific way.
The input is always a date where the month is written in French (instead of English) language.
I don't need to detect current language/culture. The first link you've mentioned (I already knew it) doesn't give me any hint to solve the problem.
Graeme_Grant 5-Feb-23 17:27pm    
You missed the point of my reply, and the reply of others. We are not going to write your code for you.

The information that you want can be found in the links provided. Part of programming is learning through various techniques, like research. If I was to write the answer for you, I too would need to use those links to work out a solution. I have pointed you in the direction that I would go...
LB2371 5-Feb-23 17:49pm    
Have you noticed the button above that says "Accept Solution" (not "Accept Lesson")?
I'm here to solve a specific problem, and I've "googled" a lot before asking my question.
I don't need a teacher.
Don't feel like you have to answer.
LB2371 6-Feb-23 2:14am    
Your solution is just like the one I've already adopted in my script. I'm rewriting it, getting rid of the months' array and of all that depends on it (split, indexOf, template literals), without any external libraries; just vanilla JS. That's my task - and that's the reason of my question here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900