Open In App

JavaScript Program for Converting given Time to Words

This article will show you how to convert the given time into words in JavaScript. The given time will be in 12-hour format (hh: mm), where 0 < hh < 12, and 0 <= mm < 60.

Examples:



Input : hh = 6, mm = 20
Output : Six Hour, Twenty Minutes
Input : hh = 8, mm = 24
Output : Eight Hour, Twenty Four Minutes

Approach:

Example: Below is the implementation of the approach






// JavaScript Program to Convert Given Time into Words
function printWords(hh, mm) {
    let words = [
        "One", "Two", "Three", "Four", "Five",
        "Six", "Seven", "Eight", "Nine", "Ten",
        "Eleven", "Twelve", "Thirteen",
        "Fourteen", "Fifteen", "Sixteen",
        "Seventeen", "Eighteen", "Nineteen",
        "Twenty", "Thirty", "Fourty", "Fifty"
    ];
  
    let minutes;
  
    if (mm < 20) {
        minutes = words[mm - 1];
    } else {
        minutes = words[(17 + Math.floor(mm / 10))]
            + " " + words[(mm % 10) - 1];
    }
  
    console.log(words[hh - 1] + " Hours, "
        + minutes + " Minutes");
}
  
let hh = 07;
let mm = 22;
printWords(hh, mm);

Output
Seven Hours, Twenty Two Minutes
Article Tags :