Quelle heure est-il? What time is it?

In this tutorial we will use conditional statements to tell us the time in the French language.

Telling time in French is just a matter of knowing the French numbers and a few formulas and rules. To tell a time that is right on the hour, simply use “Il est“, followed by the number in French, and then the word “heure” if it’s one o’clock, or “heures” for any number greater than one.

Il est un heure It’s one o’clock Il est trois heures It’s three o’clock Il est sept heures It’s seven o’clock

To tell the minute, you append the French numbers one through sixty. There are a few special cases: if it’s fifteen minutes after the hour, you say “et quart” and if it’s thirty minutes past the hour, you’d say “et midi“.

French doesn’t have words for “a.m.” and “p.m.” You can use du matin for a.m., de l’après-midi from noon until about 6 p.m., and du soir from 6 p.m. until midnight.

Let’s start coding.

First, we will create two strings with all the number-words:

var ones = "|un|deux|trois|quatre|cinq|six|sept|huit|neuf|dix|onze|douze|treize|quatorze|et quart|seize"; var tens = "|dix|vingt|trente|quarante|cinquante";

Here, we are treating the numbers 11 through 16 as “ones” since they are, in a manner of speaking, irregular. Now, we need to get the hours and minutes of the current time. To do this, first we create a new date object, then use the getHours() and getMinutes() methods to do this.

var d = new Date(); var c = d.getHours(); var mn = d.getMinutes();

We can leave the minute as it is, but the hour is always returned in “military” time, so we need to convert it to standard time. We will leave the time “12 midnight” as 0 so that the script will display “minuit” later on.

h = (c == (0|12)) ? c : c%12

Next, we’ll create a function to show the hour. First we’ll do the special cases, then the generic ones.

function fHeures(h){ if (h == 12) str = 'midi'; else if (h == 0) str = 'minuit'; else str = ones.split("|")[h%12] + " heure" + ((h%12 == 1) ? " " : "s"); return str; }

Now, we have to get the minutes:

function fMinutes(mn) { if (mn < 17) str = ones.split("|")[mn]; else if (mn == 30) str = "et demie"; else { p = mn.toString().split(''); if (p[1] == '1') str = tens.split("|")[p[0]] + " et un"; else if (p[1] == '0') str = tens.split("|")[p[0]]; else str = tens.split("|")[p[0]] + "-" + ones.split("|")[p[1]]; } return str }

As you can see, this is a bit more complicated than the hour. In this block of code, if the minute is less than 17, it simply uses split() to retrieve the appropriate word from the string ones . If it’s exactly 30 minutes after, it gives you “et demie”, and otherwise, it breaks up the minute into its two constituent numbers, and then returns the appropriate word.

At this point, all we have left to do is to return the part of the day. This can be done in a condensed format as follows:

ampm = (h%12) ? [" du matin"," de l'apres-midi"," du soir"][(h > 12) + (h > 18)] : "";

Here, (h > 12) and (h > 18) are Boolean expressions. If they are true, they will each evaluate to 1, and adding them gives us an index for the preceding array. Now all we have to do is put it all together. We will re-arrange the code slightly so that we get the time within the main function.

function fTime() { var d = new Date(); mn = d.getMinutes(); c = d.getHours(); h = (c == 0 | 12) ? c : c % 12 //if not noon or midnight, convert to standard time ampm = (h % 12) ? ["du matin", "de l'apres-midi", "du soir"][(h > 12) + (h > 18)] : ""; document.getElementById("demo").innerHTML = "il est " + fHeures(h) + fMinutes(mn) + " " + ampm;; } document.getElementById("demo").addEventListener("click", fTime);

After adding some CSS, this is the result: