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

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 ` `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 currrent year ` `    ``while` `(daysTillNow >= 365) { ` `        ``if` `(currYear % 400 == 0 ` `            ``|| (currYear % 4 == 0 ` `                ``&& currYear % 100 != 0)) { ` `            ``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 ` `        ``|| (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 currrent year ` `    ``while` `(daysTillNow >= ``365``) ` `    ``{ ` `        ``if` `(currYear % ``400` `== ``0` `||  ` `           ``(currYear % ``4` `== ``0` `&&  ` `            ``currYear % ``100` `!= ``0``)) ` `        ``{ ` `            ``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` `|| ` `       ``(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 `

## 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 currrent year ` `    ``while` `(daysTillNow >= 365) ` `    ``{ ` `        ``if` `(currYear % 400 == 0 ||  ` `           ``(currYear % 4 == 0 &&  ` `            ``currYear % 100 != 0)) ` `        ``{ ` `            ``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 || ` `       ``(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 `

Output:

```23/7/2020 9:52:36
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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 : 29AjayKumar