Search code examples
javascripttimelocale

Translate the words "Days", "Hours", "Minutes", & "Seconds" to locale equivalent


I don't want to convert numeric times to different locales, but this is what all my searches lead me to. I need to convert the actual words "Days", "Hours", etc. to the user's locale equivalent (e.g. "Jours", "Heures", etc.), and I need the abbreviations also.

Is this doable natively in JavaScript?


Solution

  • You can use Intl.DateTimeFormat():

    The Intl.DateTimeFormat() constructor for objects that enable language-sensitive date and time formatting.

    An example:

    var date = new Date();
    let o = new Intl.DateTimeFormat("fr-FR" , {
        timeStyle: "full",
        dateStyle: "full"
    });
    console.log(o.format(date));