Convert given time into words

Given a time in the format of hh:mm (12-hour format) 0 < hh < 12, 0 <= mm < 60. The task is to convert it into words as shown:

Examples :

Input : h = 5, m = 0
Output : five o' clock

Input : h = 6, m = 24
Output : twenty four minutes past six

Corner cases are m = 0, m = 15, m = 30 and m = 45.

6:00 six o'clock
6:10 ten minutes past six
6:15 quarter past six
6:30 half past six
6:45 quarter to seven
6:47 thirteen minutes to seven



The idea is to use if-else-if statement to determine the time in words. According to above given example, on the basis of minutes we can categories time in words into 8, that are minutes equal to 0, 15, 30, 45, 1, 59 and in range less than 30 or greater than 30. Check the value of minutes and print accordinly.

Below is implementation of this approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to convert time into words
#include <bits/stdc++.h>
using namespace std;
  
// Print Time in words.
void printWords(int h, int m)
{
    char nums[][64] = { "zero", "one", "two", "three", "four",
                        "five", "six", "seven", "eight", "nine",
                        "ten", "eleven", "twelve", "thirteen",
                        "fourteen", "fifteen", "sixteen", "seventeen",
                        "eighteen", "nineteen", "twenty", "twenty one",
                        "twenty two", "twenty three", "twenty four",
                        "twenty five", "twenty six", "twenty seven",
                        "twenty eight", "twenty nine",
                      };
  
    if (m == 0)
        printf("%s o' clock\n", nums[h]);
  
    else if (m == 1)
        printf("one minute past %s\n", nums[h]);
  
    else if (m == 59)
        printf("one minute to %s\n", nums[(h % 12) + 1]);
  
    else if (m == 15)
        printf("quarter past %s\n", nums[h]);
  
    else if (m == 30)
        printf("half past %s\n", nums[h]);
  
    else if (m == 45)
        printf("quarter to %s\n", nums[(h % 12) + 1]);
  
    else if (m <= 30)
        printf("%s minutes past %s\n", nums[m], nums[h]);
  
    else if (m > 30)
        printf("%s minutes to %s\n", nums[60 - m],
                                     nums[(h % 12) + 1]);
}
  
// Driven Program
int main()
{
    int h = 6;
    int m = 24;
  
    printWords(h, m);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to convert time into words
class GFG
{
      
    // Print Time in words.
    static void printWords(int h, int m)
    {
        String nums[] = { "zero", "one", "two", "three", "four",
                            "five", "six", "seven", "eight", "nine",
                            "ten", "eleven", "twelve", "thirteen",
                            "fourteen", "fifteen", "sixteen", "seventeen",
                            "eighteen", "nineteen", "twenty", "twenty one",
                            "twenty two", "twenty three", "twenty four",
                            "twenty five", "twenty six", "twenty seven",
                            "twenty eight", "twenty nine",
                        };
      
        if (m == 0)
            System.out.println(nums[h] + " o' clock ");
      
        else if (m == 1)
            System.out.println("one minute past "
                                            nums[h]);
      
        else if (m == 59)
            System.out.println("one minute to "
                            nums[(h % 12) + 1]);
      
        else if (m == 15)
            System.out.println("quarter past " + nums[h]);
      
        else if (m == 30)
            System.out.println("half past " + nums[h]);
      
        else if (m == 45)
            System.out.println("quarter to "
                                nums[(h % 12) + 1]);
      
        else if (m <= 30)
            System.out.println( nums[m] + " minutes past " +
                                                    nums[h]);
      
        else if (m > 30)
            System.out.println( nums[60 - m] + " minutes to " +     
                                                nums[(h % 12) + 1]);
    }
      
    // Driven code
    public static void main(String []args)
    {
        int h = 6;
        int m = 24;
        printWords(h, m);
    }
}
  
// This code is contributed by ihritik

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to convert time into words
using System;
  
class GFG
{
      
    // Print Time in words.
    static void printWords(int h, int m)
    {
        string [] nums = { "zero", "one", "two", "three", "four",
                            "five", "six", "seven", "eight", "nine",
                            "ten", "eleven", "twelve", "thirteen",
                            "fourteen", "fifteen", "sixteen", "seventeen",
                            "eighteen", "nineteen", "twenty", "twenty one",
                            "twenty two", "twenty three", "twenty four",
                            "twenty five", "twenty six", "twenty seven",
                            "twenty eight", "twenty nine",
                        };
      
        if (m == 0)
            Console.WriteLine(nums[h] + " o' clock ");
      
        else if (m == 1)
            Console.WriteLine("one minute past " + nums[h]);
      
        else if (m == 59)
            Console.WriteLine("one minute to " +
                                nums[(h % 12) + 1]);
      
        else if (m == 15)
            Console.WriteLine("quarter past " + nums[h]);
      
        else if (m == 30)
            Console.WriteLine("half past " + nums[h]);
      
        else if (m == 45)
            Console.WriteLine("quarter to "
                                nums[(h % 12) + 1]);
      
        else if (m <= 30)
            Console.WriteLine( nums[m] + " minutes past " +
                                                    nums[h]);
      
        else if (m > 30)
            Console.WriteLine( nums[60 - m] + " minutes to "
                                            nums[(h % 12) + 1]);
    }
      
    // Driven code
    public static void Main()
    {
        int h = 6;
        int m = 24;
        printWords(h, m);
    }
}
  
// This code is contributed by ihritik

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to convert 
// time into words
  
// Print Time in words.
function printWords($h, $m)
{
    $nums = array("zero", "one", "two", "three", "four",
                  "five", "six", "seven", "eight", "nine",
                  "ten", "eleven", "twelve", "thirteen",
                  "fourteen", "fifteen", "sixteen", "seventeen",
                  "eighteen", "nineteen", "twenty", "twenty one",
                  "twenty two", "twenty three", "twenty four",
                  "twenty five", "twenty six", "twenty seven",
                  "twenty eight", "twenty nine");
  
    if ($m == 0)
        echo $nums[$h], 
             "o' clock\n" ;
  
    else if ($m == 1)
        echo "one minute past "
                $nums[$h], "\n";
  
    else if ($m == 59)
        echo "one minute to "
             $nums[($h % 12) + 1], "\n";
  
    else if ($m == 15)
        echo "quarter past "
             $nums[$h], "\n";
  
    else if ($m == 30)
        echo "half past "
           $nums[$h],"\n";
  
    else if ($m == 45)
        echo "quarter to "
             ($nums[($h % 12) + 1]), "\n";
  
    else if ($m <= 30)
        echo $nums[$m], 
             " minutes past "
               $nums[$h],"\n";
  
    else if ($m > 30)
        echo $nums[60 - $m],
             " minutes to "
             $nums[($h % 12) + 1], "\n";
}
  
// Driver Code
$h = 6;
$m = 24;
  
printWords($h, $m);
  
// This code is contributed by aj_36
?>

chevron_right



Output :

twenty four minutes past six

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : jit_t, ihritik



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.