Linux / UNIX: Convert Epoch Seconds To the Current Time

How do I convert Epoch seconds to the current time under UNIX or Linux operating systems?

Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight proleptic Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds.

 

 

 

Print Current UNIX Time

Type the following command to display the seconds since the epoch:

date +%s

Sample outputs:
1268727836

Convert Epoch To Current Time

Type the command:

date -d @Epoch
date -d @1268727836
date -d "1970-01-01 1268727836 sec GMT"

Sample outputs:

Tue Mar 16 13:53:56 IST 2010

Please note that @ feature only works with latest version of date (GNU coreutils v5.3.0+). To convert number of seconds back to a more readable form, use a command like this:

date -d @1268727836 +"%d-%m-%Y %T %z"

Sample outputs:

16-03-2010 13:53:56 +0530
WARNING! Note that the date and awk command syntax may not work on all versions of UNIX. Please refer to your local man page.

AWK Example

echo 1268727836 | awk '{print strftime("%c",$1)}'

Perl Example

perl -e "print scalar(localtime(1268727836))"
2 (1)
Article Rating (1 Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments (1)
Comment By Dave - Sun, Jul 11th, 2021 1:51 AM
This is wrong. Title says "Linux / UNIX: Convert Epoch Seconds To the Current Time" But the examples are not UNIX compliant. They are GNU/Linux (and maybe BSD)
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
Tunneling With SSH to Your Destination
Viewed 5795 times since Wed, May 30, 2018
8 Practical Examples of Linux Xargs Command for Beginners
Viewed 6825 times since Fri, Jun 1, 2018
Script HW/SW AIX
Viewed 10557 times since Mon, Jun 4, 2018
Dealing with Funny Files
Viewed 3648 times since Wed, May 30, 2018
“Yes/No” in Bash Script – Prompt for Confirmation
Viewed 36378 times since Mon, Feb 18, 2019
30 Examples for Awk Command in Text Processing
Viewed 16918 times since Sat, Nov 24, 2018
O’Reilly’s CD bookshelf
Viewed 15719 times since Wed, Jun 27, 2018
Adding a range of IPs in Linux
Viewed 3524 times since Mon, May 21, 2018
Epoch & Unix Timestamp Conversion Tools
Viewed 84966 times since Fri, Jun 22, 2018
Bash: String Length – How To Find Out
Viewed 3148 times since Mon, Feb 18, 2019