Converting seconds into days, hours, minutes and seconds

Given an integer n(in seconds).Convert it into days, hours, minutes and seconds.

Examples:

Input : 369121517
Output : 4272 days 5 hours 45 minutes 17 seconds

Input : 129600
Output : 1 days 12 hours 0 minutes 0 seconds



Number of days = ⌊ n / (24 * 3600) ⌋
Number of Hours = ⌊ (n % (24 * 3600)) / 3600 ⌋
Number of Minutes = ⌊ (n % (24 * 3600 * 3600)) / 60 ⌋
Number of Seconds = ⌊ (n % (24 * 3600 * 3600 * 60)) / 60 ⌋

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to convert seconds
// into days, hours,minutes,seconds
#include <bits/stdc++.h>
using namespace std;
  
// function convert second into day 
// hours, minutes and seconds
void ConvertSectoDay(int n)
{
    int day = n / (24 * 3600);
  
    n = n % (24 * 3600);
    int hour = n / 3600;
  
    n %= 3600;
    int minutes = n / 60 ;
  
    n %= 60;
    int seconds = n;
      
    cout << day << " " << "days " << hour 
         << " " << "hours " << minutes << " " 
         << "minutes " << seconds << " " 
         << "seconds "  << endl;
}
   
// Driver code 
int main()
{
    // Given n is in seconds
    int n = 129600;
    ConvertSectoDay(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to convert seconds
// into days, hours,minutes,seconds
import java.io.*;
  
class GFG 
{
    // function convert second into day 
    // hours, minutes and seconds
    static void ConvertSectoDay(int n)
    {
        int day = n / (24 * 3600);
      
        n = n % (24 * 3600);
        int hour = n / 3600;
      
        n %= 3600;
        int minutes = n / 60 ;
      
        n %= 60;
        int seconds = n;
          
        System.out.println( day + " " + "days " + hour 
                           + " " + "hours " + minutes + " "
                           + "minutes " + seconds + " "
                           + "seconds ");
    }
      
    // Driver code 
    public static void main (String[] args) 
    {
        // Given n is in seconds
        int n = 129600;
        ConvertSectoDay(n);
  
    }
}
  
// This code is contributed by vt_m.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to convert seconds
# into days, hours, minutes, seconds
  
# Function convert second into day 
# hours, minutes and seconds
def ConvertSectoDay(n):
  
    day = n // (24 * 3600)
  
    n = n % (24 * 3600)
    hour = n // 3600
  
    n %= 3600
    minutes = n // 60
  
    n %= 60
    seconds = n
      
    print(day,"days", hour, "hours"
          minutes, "minutes",
          seconds, "seconds")
  
  
# Driver code 
  
# Given n is in seconds
n = 129600
ConvertSectoDay(n)
  
# This code is contributed by Anant Agarwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to convert seconds
// into days, hours,minutes,seconds
using System;
  
class GFG {
      
    // function convert second into day 
    // hours, minutes and seconds
    static void ConvertSectoDay(int n)
    {
        int day = n / (24 * 3600);
      
        n = n % (24 * 3600);
        int hour = n / 3600;
      
        n %= 3600;
        int minutes = n / 60 ;
      
        n %= 60;
        int seconds = n;
          
        Console.WriteLine( day + " " 
              + "days " + hour + " " 
          + "hours " + minutes + " "
        + "minutes " + seconds + " "
                        + "seconds ");
    }
      
    // Driver code 
    public static void Main () 
    {
          
        // Given n is in seconds
        int n = 129600;
          
        ConvertSectoDay(n);
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to convert seconds
// into days, hours,minutes,seconds
  
// function convert second into day 
// hours, minutes and seconds
function ConvertSectoDay($n)
{
    $day = floor($n / (24 * 3600));
  
    $n = ($n % (24 * 3600));
    $hour = $n / 3600;
  
    $n %= 3600;
    $minutes = $n / 60 ;
  
    $n %= 60;
    $seconds = $n;
      
echo ("$day days $hour hours $minutes minutes $seconds seconds");
          
}
  
// Driver code 
{
      
    // Given n is in seconds
    $n = 129600;
    ConvertSectoDay($n);
    return 0;
}
  
// This code is contributed by Nitin Mittal.
?>

chevron_right



Output :

1 days 12 hours 0 minutes 0 seconds 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nitin mittal



Article Tags :

1


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