Javatpoint Logo
Javatpoint Logo

Converting Linux/Unix Time to Human-Readable Date Format

The Unix time period also referred to simply as the "epoch period", the "POSIX period", or the "Unix period", is a time tracking system that represents the number of seconds that have elapsed since the Unix period of the Unix period started on February 1, 1970, at 00:00. 00:00 UTC is defined.

Unix Time begins at midnight on January 1, 1970, UTC. It counts the total number of seconds that have passed since then. For example, the Unix timestamp of 1716620142 represents the number of seconds that have elapsed since January 1, 1970. The Unix epoch period has been largely calculated for various purposes, including file systems, databases, and network protocols, because it is simple and provides a consistent way of dealing with dates and seasons.

Converting Linux/Unix Time to Human-Readable Date Format

The Unix epoch time does not account for leap seconds. It assumes that each day is exactly 86400 seconds long, simplifying the calculations but introducing some errors over time. It provides consistent measurement time in systems. Using a single integer value to represent time simplifies time calculations and comparisons. Transitions between Unix time and human-readable dates can be performed using Linux Bash Commands and programming languages. For example, the date command can be used on Unix-like systems, and most programming languages provide libraries to handle such conversions.

Using Command in Linux/Unix

In Linux, you can utilize the date command to convert the Linux time to a human-readable format.

Open the terminal and run the below-given command to generate a Unix timestamp:

Command

Output

Converting Linux/Unix Time to Human-Readable Date Format

Now, to convert it into a human-readable format, run the below command:

Command:

Here, input the generated timestamp from the earlier command. Like this,

Output:

Converting Linux/Unix Time to Human-Readable Date Format

In Mac OS

In macOS, the date command syntax is a little different.

Use the below command to generate a timestamp:

Command

Output

Converting Linux/Unix Time to Human-Readable Date Format

To convert into human-readable format, execute the below command

Command

Replace the <timestamp> with the generated timestamp.

Output

Converting Linux/Unix Time to Human-Readable Date Format

Converting Seconds into DateTime Format

  • First, divide the total number of seconds into larger units, such as days, hours, minutes, and remaining seconds.
  • Then, map these units to the Gregorian calendar to get the year, month, day, hour, minute, and second.
  • Calculate Total Days: Convert seconds to days by dividing the total seconds by the number of seconds in a day (86400).
  • Determine the Year, Month and Day: Starting from 1970, add years until the total number of days exceeds the number of days in each year. Subtract the number of days for each year as you go. Once the year is determined, use the remaining days to find the month and day. Subtract the days of each month until the remaining days fit into the current month.
  • Calculate the Time of Day: Convert the remaining seconds into hours, minutes, and seconds.

Example

Let's understand by an example. Here, we have taken an example of 1716547702 seconds.

Total Seconds = 1716547702

Total Seconds in a Day: 86400

Convert to Days:

Total Days = 1716547702 / 86400 = 19867

Remaining Seconds: 1716547702 % 86400 = 41102

Hours, Minutes and Seconds

1 Hour = 3600 Seconds

1 Minute = 60 Seconds

Hours: 41200 / 3600 = 11

Remaining Seconds: 41102 % 3600 = 4102

Minutes = 4102 / 60 = 68

Seconds = 4102 % 60 = 2

Determine Year:

Starting from 1970, subtract the number of days in each year until the total days fit into the current year.

Calculation of Leap and Non-Leap years:

  • 1970: Non Leap Year (365 Days)
  • 1971: Non-Leap year (365 Days)
  • 1972: Leap year (366 Days)

This process will continue until the year is determined.

Month and Day:

Identify days in each month and subtract days for each month.

Calculation of Month and Day

January: 31 Days

February: 28 Days

March: 31 Days

This process will continue until the month is determined.

Calculate the time of day:

Convert the remaining seconds into hours, minutes and seconds.

Hours = 11

Minutes = 68

Seconds: 2

Implementation of the Above Approach Using Javascript

We have used the approach in the JavaScript we saw above here. Let's see the program.

Code

Output:

 
Human-Readable DateTime: 2024-05-24 10:48:22

Explanation

The 'convertUnixToReadable' function in the above JS code snippet converts the Unix timestamp to a human-readable date and time format. Next, the code starts by using a 'Date' object to convert the Unix timestamp from seconds to milliseconds. The program then extracts the year, month, day, hours, minutes and seconds from the 'Date' object and inserts leading zeros where needed to ensure that each element is two digits and then these parts are 'YYYY-'. MM-DD HH:MM :. it is formatted as a string in SS format and returned.

Using Python

Here is the complete Python code in which we have implemented the epoch approach.

Code

Output:

 
Human-Readable DateTime Format: 2024-05-24 10:48:22

Explanation

In the above Python code, first calculate all the days, hours, minutes, and seconds of a given Unix timestamp and then repeat since 1970 to account for overages. Subtract the days until the remaining days are less than the days of the current year. Subtract the days in each month from the remaining days, and the month and day are calculated by adjusting the excess years of the account. Finally, the calculated year, month, day, hour, minute, and second are formatted into an easy-to-read string and printed as a result.

Using Java

Here is the complete Java code in which we have implemented the epoch approach.

Code

Output:

 
Human Readable DateTime: 2024-05-24 16:18:22

Explanation

The provided Java code converts Unix timestamps to a human-readable date-time format by defining the first timestamp using 'Instant.ofEpochSecond' with the system's default timestamp as a 'LocalDateTime' object. It then converts this 'LocalDateTime' object to yyyy-MM-dd HH:mm:. It formats human-readable to a string by using 'DateTimeFormatter' with its counterpart ss'. Finally, it prints an array of date-time strings.

Using C++

Here is the complete C++ code in which we have implemented the epoch approach.

Code

Output:

 
Human Readable Date-Time: 2024-05-24 10:48:22

Explanation

In the above code, define the first timestamp and change it to 'std::chrono::system_clock::time_point'. Then, this time point is 'std::time_t' to use with 'std::gmtime'. They are converted and formatted to a string readable by 'std::put_time' to specify the date and time in UTC.

Using C

Here is the complete C code in which we have implemented the epoch approach.

Code

Output:

 
Human Readable Date-Time: 2024-05-24 10:48:22

Explanation

In the above C code, defining the first timestamp and using the 'gmtime' function to modify a standing 'struct tm' for UTC time and then 'strftime to get the "YYYY- MM-DD HH:MM:SS" format Formats this time to the string used '.

Using C#

Here is the complete C# code in which we have implemented the epoch approach.

Code

Output:

 
Human Readable Date-Time: 2024-05-24 10:48:22

Explanation

In the given C# code, define a timestamp and convert it to a 'DateTimeOffset' using the 'FromUnixTimeSeconds' method and then setting this 'DateTimeOffset' go to a string with the format "yyyy- MM-dd HH:mm:ss". Additionally, the code manually calculates all days, hours, minutes, and seconds from the Unix timestamp, including the remaining hours and minutes when the days and hours are divided Finally, it uses a date-time string listed with a detailed breakdown of the timestamps, including components.

Using PHP

Here is the complete C# code in which we have implemented the epoch approach.

Code

Output:

 
Human Readable Date-Time: 2024-05-24 10:48:22

Explanation

In the above PHP code, we have converted the Unix timestamp to a human-readable date-time format using an interpreter. It then converts it to a 'DateTime' object using the 'DateTime' constructor, which uses a Unix timestamp set to " . @" overrides the face and then inverts this 'DateTime' against "Y-m-d H:i:s." It formats the string in format.

Conclusion

Converting Linux/Unix time to a human-readable date format is necessary for defining timestamps representing seconds since January 1, 1970. This conversion is also necessary for analyzing logs, viewing file types, and creating time-dependent data-driven applications. This ensures that Temporal information is accessible and meaningful and facilitates efficient debugging, data analysis, and communication. By converting Unix time to a standard date and time format, we make the data more meaningful and actionable for users and programs.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA