Open In App

Convert Unix timestamp to DD/MM/YYYY HH:MM:SS format

Last Updated : 06 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a Unix Timestamp T (in seconds) for a given point in time, the task is to convert it to human-readable format (DD/MM/YYYY HH:MM:SS)

Example:

Input: T = 1595497956
Output:23/7/2020 9:52:36
Explanation:In unix time T have 1595497956 seconds, So it makes total 50 years, 7 months, 23 days and 9 hours, 52 minutes and 36 second.

Input: T = 345234235
Output:9/12/1980 18:23:55

Approach:

  1. Convert given seconds into days by dividing them by the number of seconds in a day (86400) and store the remaining second.
  2. Since we count the number of days since Jan 1, 1970. Therefore, to calculate the current year keeping the concept of leap years in mind. Start in 1970. If the year is a leap year subtract 366 from days, otherwise subtract 365. Increase year by 1.
  3. Repeat step 2 until days become less than 365 (can not constitute a year).
  4. Add 1 to remaining days (extra days after calculating year) because the remaining days will give us days till the previous day, and we have to include the current day for DATE and MONTH calculation.
  5. Increment the number of months by 1 and keep subtracting the number of days of the month from extra days (keeping in mind that February will have 29 days in a leap year and 28 otherwise).
  6. Repeat steps 5 until subtracting days of the month from extra days will give a negative result.
  7. If extra days are more than zero, increment month by 1.
  8. Now make use of the extra time from step 1.
  9. Calculate hours by dividing extra time by 3600, minutes by dividing the remaining seconds by 60, and seconds will be the remaining seconds.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
// Unix time is in seconds and
// Humar Readable Format:
// DATE:MONTH:YEAR:HOUR:MINUTES:SECONDS,
// Start of unix time:01 Jan 1970, 00:00:00
#include <bits/stdc++.h>
using namespace std;
 
// Function to convert unix time to
// Human readable format
string unixTimeToHumanReadable(long int seconds)
{
 
    // Save the time in Human
    // readable format
    string ans = "";
 
    // Number of days in month
    // in normal year
    int daysOfMonth[] = { 31, 28, 31, 30, 31, 30,
                          31, 31, 30, 31, 30, 31 };
 
    long int currYear, daysTillNow, extraTime, extraDays,
        index, date, month, hours, minutes, secondss,
        flag = 0;
 
    // Calculate total days unix time T
    daysTillNow = seconds / (24 * 60 * 60);
    extraTime = seconds % (24 * 60 * 60);
    currYear = 1970;
 
    // Calculating current year
    while (true) {
        if (currYear % 400 == 0
            || (currYear % 4 == 0 && currYear % 100 != 0)) {
            if (daysTillNow < 366) {
                break;
            }
            daysTillNow -= 366;
        }
        else {
            if (daysTillNow < 365) {
                break;
            }
            daysTillNow -= 365;
        }
        currYear += 1;
    }
    // Updating extradays because it
    // will give days till previous day
    // and we have include current day
    extraDays = daysTillNow + 1;
 
    if (currYear % 400 == 0
        || (currYear % 4 == 0 && currYear % 100 != 0))
        flag = 1;
 
    // Calculating MONTH and DATE
    month = 0, index = 0;
    if (flag == 1) {
        while (true) {
 
            if (index == 1) {
                if (extraDays - 29 < 0)
                    break;
                month += 1;
                extraDays -= 29;
            }
            else {
                if (extraDays - daysOfMonth[index] < 0) {
                    break;
                }
                month += 1;
                extraDays -= daysOfMonth[index];
            }
            index += 1;
        }
    }
    else {
        while (true) {
 
            if (extraDays - daysOfMonth[index] < 0) {
                break;
            }
            month += 1;
            extraDays -= daysOfMonth[index];
            index += 1;
        }
    }
 
    // Current Month
    if (extraDays > 0) {
        month += 1;
        date = extraDays;
    }
    else {
        if (month == 2 && flag == 1)
            date = 29;
        else {
            date = daysOfMonth[month - 1];
        }
    }
 
    // Calculating HH:MM:YYYY
    hours = extraTime / 3600;
    minutes = (extraTime % 3600) / 60;
    secondss = (extraTime % 3600) % 60;
 
    ans += to_string(date);
    ans += "/";
    ans += to_string(month);
    ans += "/";
    ans += to_string(currYear);
    ans += " ";
    ans += to_string(hours);
    ans += ":";
    ans += to_string(minutes);
    ans += ":";
    ans += to_string(secondss);
 
    // Return the time
    return ans;
}
 
// Driver Code
int main()
{
    // Given unix time
    long int T = 1595497956;
 
    // Function call to convert unix
    // time to human read able
    string ans = unixTimeToHumanReadable(T);
 
    // Print time in format
    // DD:MM:YYYY:HH:MM:SS
    cout << ans << "\n";
 
    return 0;
}


Java




// Java program for the above approach
 
// Unix time is in seconds and
// Humar Readable Format:
// DATE:MONTH:YEAR:HOUR:MINUTES:SECONDS,
// Start of unix time:01 Jan 1970, 00:00:00
import java.util.*;
 
class GFG {
 
    // Function to convert unix time to
    // Human readable format
    static String unixTimeToHumanReadable(int seconds)
    {
 
        // Save the time in Human
        // readable format
        String ans = "";
 
        // Number of days in month
        // in normal year
        int daysOfMonth[] = { 31, 28, 31, 30, 31, 30,
                              31, 31, 30, 31, 30, 31 };
 
        int currYear, daysTillNow, extraTime, extraDays,
            index, date, month, hours, minutes, secondss,
            flag = 0;
 
        // Calculate total days unix time T
        daysTillNow = seconds / (24 * 60 * 60);
        extraTime = seconds % (24 * 60 * 60);
        currYear = 1970;
 
        // Calculating current year
        while (true) {
        if (currYear % 400 == 0
            || (currYear % 4 == 0 && currYear % 100 != 0)) {
            if (daysTillNow < 366) {
                break;
            }
            daysTillNow -= 366;
        }
        else {
            if (daysTillNow < 365) {
                break;
            }
            daysTillNow -= 365;
        }
        currYear += 1;
    }
 
        // Updating extradays because it
        // will give days till previous day
        // and we have include current day
        extraDays = daysTillNow + 1;
 
        if (currYear % 400 == 0
            || (currYear % 4 == 0 && currYear % 100 != 0))
            flag = 1;
 
        // Calculating MONTH and DATE
        month = 0;
        index = 0;
        if (flag == 1) {
            while (true) {
                if (index == 1) {
                    if (extraDays - 29 < 0)
                        break;
 
                    month += 1;
                    extraDays -= 29;
                }
                else {
                    if (extraDays - daysOfMonth[index]
                        < 0) {
                        break;
                    }
                    month += 1;
                    extraDays -= daysOfMonth[index];
                }
                index += 1;
            }
        }
        else {
            while (true) {
                if (extraDays - daysOfMonth[index] < 0) {
                    break;
                }
                month += 1;
                extraDays -= daysOfMonth[index];
                index += 1;
            }
        }
 
        // Current Month
        if (extraDays > 0) {
            month += 1;
            date = extraDays;
        }
        else {
            if (month == 2 && flag == 1)
                date = 29;
            else {
                date = daysOfMonth[month - 1];
            }
        }
 
        // Calculating HH:MM:YYYY
        hours = extraTime / 3600;
        minutes = (extraTime % 3600) / 60;
        secondss = (extraTime % 3600) % 60;
 
        ans += String.valueOf(date);
        ans += "/";
        ans += String.valueOf(month);
        ans += "/";
        ans += String.valueOf(currYear);
        ans += " ";
        ans += String.valueOf(hours);
        ans += ":";
        ans += String.valueOf(minutes);
        ans += ":";
        ans += String.valueOf(secondss);
 
        // Return the time
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Given unix time
        int T = 1595497956;
 
        // Function call to convert unix
        // time to human read able
        String ans = unixTimeToHumanReadable(T);
 
        // Print time in format
        // DD:MM:YYYY:HH:MM:SS
        System.out.print(ans + "\n");
    }
}
 
// This code is contributed by 29AjayKumar


Python3




# Python3 program for the above approach
 
# Unix time is in seconds and
# Humar Readable Format:
# DATE:MONTH:YEAR:HOUR:MINUTES:SECONDS,
# Start of unix time:01 Jan 1970, 00:00:00
 
# Function to convert unix time to
# Human readable format
 
 
def unixTimeToHumanReadable(seconds):
 
    # Save the time in Human
    # readable format
    ans = ""
 
    # Number of days in month
    # in normal year
    daysOfMonth = [31, 28, 31, 30, 31, 30,
                   31, 31, 30, 31, 30, 31]
 
    (currYear, daysTillNow, extraTime,
     extraDays, index, date, month, hours,
     minutes, secondss, flag) = (0, 0, 0, 0, 0,
                                 0, 0, 0, 0, 0, 0)
 
    # Calculate total days unix time T
    daysTillNow = seconds // (24 * 60 * 60)
    extraTime = seconds % (24 * 60 * 60)
    currYear = 1970
 
    # Calculating current year
    while (daysTillNow >= 365):
        if (currYear % 400 == 0 or
            (currYear % 4 == 0 and
                currYear % 100 != 0)):
          if daysTillNow < 366:
            break
          daysTillNow -= 366
 
        else:
            daysTillNow -= 365
 
        currYear += 1
 
    # Updating extradays because it
    # will give days till previous day
    # and we have include current day
    extraDays = daysTillNow + 1
 
    if (currYear % 400 == 0 or
        (currYear % 4 == 0 and
            currYear % 100 != 0)):
        flag = 1
 
    # Calculating MONTH and DATE
    month = 0
    index = 0
 
    if (flag == 1):
        while (True):
 
            if (index == 1):
                if (extraDays - 29 < 0):
                    break
 
                month += 1
                extraDays -= 29
 
            else:
                if (extraDays - daysOfMonth[index] < 0):
                    break
 
                month += 1
                extraDays -= daysOfMonth[index]
 
            index += 1
 
    else:
        while (True):
            if (extraDays - daysOfMonth[index] < 0):
                break
 
            month += 1
            extraDays -= daysOfMonth[index]
            index += 1
 
    # Current Month
    if (extraDays > 0):
        month += 1
        date = extraDays
 
    else:
        if (month == 2 and flag == 1):
            date = 29
        else:
            date = daysOfMonth[month - 1]
 
    # Calculating HH:MM:YYYY
    hours = extraTime // 3600
    minutes = (extraTime % 3600) // 60
    secondss = (extraTime % 3600) % 60
 
    ans += str(date)
    ans += "/"
    ans += str(month)
    ans += "/"
    ans += str(currYear)
    ans += " "
    ans += str(hours)
    ans += ":"
    ans += str(minutes)
    ans += ":"
    ans += str(secondss)
 
    # Return the time
    return ans
 
 
# Driver code
if __name__ == "__main__":
 
    # Given unix time
    T = 1595497956
 
    # Function call to convert unix
    # time to human read able
    ans = unixTimeToHumanReadable(T)
 
    # Print time in format
    # DD:MM:YYYY:HH:MM:SS
    print(ans)
 
# This code is contributed by rutvik_56


C#




// C# program for the above approach
 
// Unix time is in seconds and
// Humar Readable Format:
// DATE:MONTH:YEAR:HOUR:MINUTES:SECONDS,
// Start of unix time:01 Jan 1970, 00:00:00
using System;
 
class GFG {
 
    // Function to convert unix time to
    // Human readable format
    static String unixTimeToHumanReadable(int seconds)
    {
 
        // Save the time in Human
        // readable format
        String ans = "";
 
        // Number of days in month
        // in normal year
        int[] daysOfMonth = { 31, 28, 31, 30, 31, 30,
                              31, 31, 30, 31, 30, 31 };
 
        int currYear, daysTillNow, extraTime, extraDays,
            index, date, month, hours, minutes, secondss,
            flag = 0;
 
        // Calculate total days unix time T
        daysTillNow = seconds / (24 * 60 * 60);
        extraTime = seconds % (24 * 60 * 60);
        currYear = 1970;
 
        // Calculating current year
        while (true) {
        if (currYear % 400 == 0
            || (currYear % 4 == 0 && currYear % 100 != 0)) {
            if (daysTillNow < 366) {
                break;
            }
            daysTillNow -= 366;
        }
        else {
            if (daysTillNow < 365) {
                break;
            }
            daysTillNow -= 365;
        }
        currYear += 1;
    }
 
        // Updating extradays because it
        // will give days till previous day
        // and we have include current day
        extraDays = daysTillNow + 1;
 
        if (currYear % 400 == 0
            || (currYear % 4 == 0 && currYear % 100 != 0))
            flag = 1;
 
        // Calculating MONTH and DATE
        month = 0;
        index = 0;
        if (flag == 1) {
            while (true) {
                if (index == 1) {
                    if (extraDays - 29 < 0)
                        break;
 
                    month += 1;
                    extraDays -= 29;
                }
                else {
                    if (extraDays - daysOfMonth[index]
                        < 0) {
                        break;
                    }
                    month += 1;
                    extraDays -= daysOfMonth[index];
                }
                index += 1;
            }
        }
        else {
            while (true) {
                if (extraDays - daysOfMonth[index] < 0) {
                    break;
                }
                month += 1;
                extraDays -= daysOfMonth[index];
                index += 1;
            }
        }
 
        // Current Month
        if (extraDays > 0) {
            month += 1;
            date = extraDays;
        }
        else {
            if (month == 2 && flag == 1)
                date = 29;
            else {
                date = daysOfMonth[month - 1];
            }
        }
 
        // Calculating HH:MM:YYYY
        hours = extraTime / 3600;
        minutes = (extraTime % 3600) / 60;
        secondss = (extraTime % 3600) % 60;
 
        ans += String.Join("", date);
        ans += "/";
        ans += String.Join("", month);
        ans += "/";
        ans += String.Join("", currYear);
        ans += " ";
        ans += String.Join("", hours);
        ans += ":";
        ans += String.Join("", minutes);
        ans += ":";
        ans += String.Join("", secondss);
 
        // Return the time
        return ans;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
 
        // Given unix time
        int T = 1595497956;
 
        // Function call to convert unix
        // time to human read able
        String ans = unixTimeToHumanReadable(T);
 
        // Print time in format
        // DD:MM:YYYY:HH:MM:SS
        Console.Write(ans + "\n");
    }
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
    // Javascript program for the above approach
     
    // Unix time is in seconds and
    // Humar Readable Format:
    // DATE:MONTH:YEAR:HOUR:MINUTES:SECONDS,
    // Start of unix time:01 Jan 1970, 00:00:00
     
    // Function to convert unix time to
    // Human readable format
    function unixTimeToHumanReadable(seconds)
    {
 
        // Save the time in Human
        // readable format
        let ans = "";
 
        // Number of days in month
        // in normal year
        let daysOfMonth = [ 31, 28, 31, 30, 31, 30,
                              31, 31, 30, 31, 30, 31 ];
 
        let currYear, daysTillNow, extraTime,
            extraDays, index, date, month, hours,
            minutes, secondss, flag = 0;
 
        // Calculate total days unix time T
        daysTillNow = parseInt(seconds / (24 * 60 * 60), 10);
        extraTime = seconds % (24 * 60 * 60);
        currYear = 1970;
 
        // Calculating current year
        while (true) {
        if (currYear % 400 == 0
            || (currYear % 4 == 0 && currYear % 100 != 0)) {
            if (daysTillNow < 366) {
                break;
            }
            daysTillNow -= 366;
        }
        else {
            if (daysTillNow < 365) {
                break;
            }
            daysTillNow -= 365;
        }
        currYear += 1;
    }
 
        // Updating extradays because it
        // will give days till previous day
        // and we have include current day
        extraDays = daysTillNow + 1;
 
        if (currYear % 400 == 0 ||
           (currYear % 4 == 0 &&
            currYear % 100 != 0))
            flag = 1;
 
        // Calculating MONTH and DATE
        month = 0; index = 0;
        if (flag == 1)
        {
            while (true)
            {
                if (index == 1)
                {
                    if (extraDays - 29 < 0)
                        break;
 
                    month += 1;
                    extraDays -= 29;
                }
                else
                {
                    if (extraDays -
                        daysOfMonth[index] < 0)
                    {
                        break;
                    }
                    month += 1;
                    extraDays -= daysOfMonth[index];
                }
                index += 1;
            }
        }
        else
        {
            while (true)
            {
                if (extraDays - daysOfMonth[index] < 0)
                {
                    break;
                }
                month += 1;
                extraDays -= daysOfMonth[index];
                index += 1;
            }
        }
 
        // Current Month
        if (extraDays > 0)
        {
            month += 1;
            date = extraDays;
        }
        else
        {
            if (month == 2 && flag == 1)
                date = 29;
            else
            {
                date = daysOfMonth[month - 1];
            }
        }
 
        // Calculating HH:MM:YYYY
        hours = parseInt(extraTime / 3600, 10);
        minutes = parseInt((extraTime % 3600) / 60, 10);
        secondss = parseInt((extraTime % 3600) % 60, 10);
 
        ans += date.toString();
        ans += "/";
        ans += month.toString();
        ans += "/";
        ans += currYear.toString();
        ans += " ";
        ans += hours.toString();
        ans += ":";
        ans += minutes.toString();
        ans += ":";
        ans += secondss.toString();
 
        // Return the time
        return ans;
    }
     
    // Given unix time
    let T = 1595497956;
  
    // Function call to convert unix
    // time to human read able
    let ans = unixTimeToHumanReadable(T);
  
    // Print time in format
    // DD:MM:YYYY:HH:MM:SS
    document.write(ans + "</br>");
 
// This code is contributed by decode2207.
</script>


Output: 

23/7/2020 9:52:36

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads