- How can I get a formatted date for a UNIX timestamp from the command line
- 8 Answers 8
- You must log in to answer this question.
- Related
- Hot Network Questions
- Subscribe to RSS
- How Can I Generate UNIX Timestamps in Linux
- What is a Unix Timestamp
- How to Convert Unix Timestamp to Date
- Method 1: Using the date Command
- Example 1: Convert a Unix Timestamp to a Date and Time in a Specific Time Zone
- Example 2: Convert a Unix Timestamp to a Date Only
- Method 2: Using the Perl Programming Language
- Method 3: Using the Python Programming Language
- Method 4: Using a Bash Script
- Conclusion
- About the author
- Kashif
- How to display Unix time in the timestamp format?
- You must log in to answer this question.
- Related
- Hot Network Questions
- Subscribe to RSS
How can I get a formatted date for a UNIX timestamp from the command line
I have a UNIX timestamp and I’d like to get a formatted date (like the output of date ) corresponding to that timestamp. My attempts so far:
$ date +%s 1282367908 $ date -d 1282367908 date: invalid date `1282367908' $ date -d +1282367908 date: invalid date `+1282367908' $ date +%s -d +1282367908 date: invalid date `+1282367908'
$ TZ=UTC somecommand 1282368345 Sat Aug 21 05:25:45 UTC 2010
8 Answers 8
$ date -r 1282368345 Sat Aug 21 07:25:45 CEST 2010 $ date -r 1282368345 +%Y-%m-%d 2010-08-21
with GNU core tools (you have to dig through the info file for that):
$ date -d @1282368345 Sat Aug 21 07:25:45 CEST 2010 $ date -d @1282368345 --rfc-3339=date 2010-08-21
With either, add the -u (standard) option, or pass a TZ=UTC0 environment variable to have the UTC date ( TZ=UTC0 defines a timezone called UTC with offset 0 from UTC while the behaviour for TZ=UTC (with no offset) is unspecified (though on most systems would refer to a system-defined timezone also called UTC with offset 0 from UTC)).
This is giving me a slightly different date: date -r 1447264553943 results in Wed Dec 28 01:39:03 BRST 47831 , when it’s actually 11/11/2015, 3:55:53 PM GMT-2:00 .
@hop ops, should have figured with the year being 47831, but that number was so high I missed it. Thanks!
After some googling, I found way to do it with the date command only:
$ date --date "Jan 1, 1970 00:00:00 +0000 + 1282367908 seconds" Sat Aug 21 09:18:28 MSD 2010
another great example for how google makes you stupid. does nobody ever read the documentation anymore?
@hop, support for date -d @xxx was not added until coreutils 5.3 in 2005. That’s how you had to do it before that.
@StéphaneChazelas: a) this answer is from 2010 and b) why does this excuse googling before reading the documentation? If you answer from your own authority, one can excuse making errors like this, but if you have to look it up anyway, look it up right.
This perl one-liner will do it:
$ perl -e 'print scalar localtime $ARGV[0]' 1282367908 Sat Aug 21 09:18:28 2010
$ zmodload zsh/datetime $ TZ=UTC0 strftime %c 1282368345 Sat 21 Aug 2010 05:25:45 UTC
$ TZ=UTC0 printf '%(%c)T\n' '#1282368345' Sat Aug 21 05:25:45 2010
$ TZ=UTC0 printf '%(%c)T\n' 1282368345 Sat 21 Aug 2010 05:25:45 UTC
In all those, replace %c with the strftime() format you want.
Answer to an old question, but I think this might be an improvement if anyone else searches for this. This Bash function works on both Linux and OS X. I have not tested on any other BSD systems. Pass the epoch time as an argument to this function and it will print the RFC-3339 time format.
epochtorfc3339 () < RFC3339_FORMAT="+%FT%T. %z" EPOCH=$(echo $@ | sed -n "s/.*\(1\\).*/\1/p"); if date --version >/dev/null 2>/dev/null; then # Linux date $ -d "1970-01-01 UTC $ seconds" #date -d @$ --rfc-3339=seconds else # OS X/BSD date -r $ $ fi >
Another neat example of the rich heritage of modern Unix. This is indeed possible under most BSD variants:
$ TZ=UTC date -r 1282368345 Sat Aug 21 05:25:45 UTC 2010
(BTW your example seems to be off by one second)
Doesn’t work on Linux. hop’s answer seems to have the correct Linux answer. Thanks for pointing that out, I suspected something like that would happen, I’ve made it 45 seconds now.
For Unix-like environment, the following will work.
# Current UNIXTIME unixtime() < datetime2unixtime "$(date -u +'%Y-%m-%d %H:%M:%S')" ># From DateTime(%Y-%m-%d %H:%M:%S)to UNIXTIME datetime2unixtime() < set -- "$" "$" set -- "$" "$" "$" "$" set -- "$1" "$" "$" "$3" "$" "$" set -- "$1" "$" "$" "$" "$" "$" [ "$2" -lt 3 ] && set -- $(( $1-1 )) $(( $2+12 )) "$3" "$4" "$5" "$6" set -- $(( (365*$1)+($1/4)-($1/100)+($1/400) )) "$2" "$3" "$4" "$5" "$6" set -- "$1" $(( (306*($2+1)/10)-428 )) "$3" "$4" "$5" "$6" set -- $(( ($1+$2+$3-719163)*86400+$4*3600+$5*60+$6 )) echo "$1" > # From UNIXTIME to DateTime format(%Y-%m-%d %H:%M:%S) unixtime2datetime() < set -- $(( $1%86400 )) $(( $1/86400+719468 )) 146097 36524 1461 set -- "$1" "$2" $(( $2-(($2+2+3*$2/$3)/$5)+($2-$2/$3)/$4-(($2+1)/$3) )) set -- "$1" "$2" $(( $3/365 )) set -- "$@" $(( $2-( (365*$3)+($3/4)-($3/100)+($3/400) ) )) set -- "$@" $(( ($4-($4+20)/50)/30 )) set -- "$@" $(( 12*$3+$5+2 )) set -- "$1" $(( $6/12 )) $(( $6%12+1 )) $(( $4-(30*$5+3*($5+4)/5-2)+1 )) set -- "$2" "$3" "$4" $(( $1/3600 )) $(( $1%3600 )) set -- "$1" "$2" "$3" "$4" $(( $5/60 )) $(( $5%60 )) printf "%04d-%02d-%02d %02d:%02d:%02d\n" "$@" ># Examples unixtime # => Current UNIXTIME date +%s # Linux command datetime2unixtime "2020-07-01 09:03:13" # => 1593594193 date -u +%s --date "2020-07-01 09:03:13" # Linux command unixtime2datetime "1593594193" # => 2020-07-01 09:03:13 date -u --date @1593594193 +"%Y-%m-%d %H:%M:%S" # Linux command
You must log in to answer this question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533
Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How Can I Generate UNIX Timestamps in Linux
In Unix timestamp we represent any date and time together in a single long number. This single number describes the total seconds passed since January 1st,1970 at 00:00:00 UTC. In this guide, we’ll cover everything about converting Unix timestamps to dates, including what Unix timestamps are, how to convert them to a date, and some practical examples to help you get started.
Content for this article is:
What is a Unix Timestamp
A Unix timestamp shows a single number which corresponds to date and time. This single number shows the total seconds that have passed since January 1st,1970 at 00:00:00 UTC. We also called this single number time the Unix epoch. Unix timestamps are used in many different programming languages to represent dates and times in any time zone.
How to Convert Unix Timestamp to Date
In Linux there are different methods to convert a Unix Timestamp to a date or vice versa. In this article we will start from the basic method which is using the date command in the terminal window.
- Using date Command
- Using the Perl Programming Language
- Using the Python Programming Language
- Using a Bash Script
Method 1: Using the date Command
To convert a Unix timestamp to a human-readable date and time, we can use the date command in the terminal. The syntax for the date command is as follows:
For example, to convert a Unix timestamp of 1676865654 to a human-readable date and time run the following command:
As you can see, the output includes complete details of current date and time including time zone, and the year.
Similarly, we can also generate timestamp value for a specific date. For example, timestamp value for January 1, 2023, at 12:00:00 AM can be get using the following command:
Following command will return UNIX timestamps for current date in nanoseconds format:
Example 1: Convert a Unix Timestamp to a Date and Time in a Specific Time Zone
To convert a Unix timestamp to a date and time in a specific time zone, following command syntax will be followed:
For example, to convert a Unix timestamp of 1613475901 to a human-readable date and time in the Eastern Time zone, run below command:
Example 2: Convert a Unix Timestamp to a Date Only
To convert a Unix timestamp to a date only, you can use the following command:
For example, if we have a Unix timestamp of 1613475901 and to convert it in the format of year-month-day (e.g. 2023-02-20) we can use following command:
This will output the date corresponding to the Unix timestamp of 1613475901:
Method 2: Using the Perl Programming Language
Perl is a popular programming language that can be used to generate UNIX timestamps in Linux.
Now create a simple Perl script that generates a UNIX timestamp for the current date and time:
Press Ctrl + O, then save this script as “timestamp.pl” and hit Enter, then press Ctrl + X to save and exit:
Now make this executable by running:
Run the script with the command “./timestamp.pl” to generate a UNIX timestamp:
Method 3: Using the Python Programming Language
Python is another popular programming language that can be used to generate UNIX timestamps in Linux.
First, we have to install Python3 on Linux, to do that run command:
Now open nano editor using:
Create a simple Python script that generates a UNIX timestamp for the current date and time:
Press Ctrl + O, then save this script as “timestamp.py” and hit Enter, then press Ctrl + X to save and exit.
The above script can be made executable by below command:
Run the script with the command “./timestamp.py” to generate a UNIX timestamp.
Method 4: Using a Bash Script
If you need to generate UNIX timestamps in a more complex or automated way, you can use a Bash script.
Open the nano editor using:
Write following script in editor that generates a UNIX timestamp for the current date and time:
Press Ctrl + O, then save this script as “timestamp.sh” and hit Enter, then press Ctrl + X to save and exit:
Now make this script executable by running the command:
Run the script with the command “./timestamp.sh” to generate a UNIX timestamp:
Conclusion
UNIX timestamps show total seconds passed since January 1, 1970. To generate UNIX timestamps in Linux, the date command can be used in the command line by passing the +%s argument. Alternatively, we can also create a bash script or use the pearl and python language script to give us timestamps for exact date and time zone.
About the author
Kashif
I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.
How to display Unix time in the timestamp format?
srand without a value uses the current timestamp with these Awk implementations:
The following will convert Date Time to Unix time on Unix-like environment.
# Current UNIXTIME unixtime() < datetime2unixtime "$(date -u +'%Y-%m-%d %H:%M:%S')" ># From DateTime(%Y-%m-%d %H:%M:%S)to UNIXTIME datetime2unixtime() < set -- "$" "$" set -- "$" "$" "$" "$" set -- "$1" "$" "$" "$3" "$" "$" set -- "$1" "$" "$" "$" "$" "$" [ "$2" -lt 3 ] && set -- $(( $1-1 )) $(( $2+12 )) "$3" "$4" "$5" "$6" set -- $(( (365*$1)+($1/4)-($1/100)+($1/400) )) "$2" "$3" "$4" "$5" "$6" set -- "$1" $(( (306*($2+1)/10)-428 )) "$3" "$4" "$5" "$6" set -- $(( ($1+$2+$3-719163)*86400+$4*3600+$5*60+$6 )) echo "$1" > # From UNIXTIME to DateTime format(%Y-%m-%d %H:%M:%S) unixtime2datetime() < set -- $(( $1%86400 )) $(( $1/86400+719468 )) 146097 36524 1461 set -- "$1" "$2" $(( $2-(($2+2+3*$2/$3)/$5)+($2-$2/$3)/$4-(($2+1)/$3) )) set -- "$1" "$2" $(( $3/365 )) set -- "$@" $(( $2-( (365*$3)+($3/4)-($3/100)+($3/400) ) )) set -- "$@" $(( ($4-($4+20)/50)/30 )) set -- "$@" $(( 12*$3+$5+2 )) set -- "$1" $(( $6/12 )) $(( $6%12+1 )) $(( $4-(30*$5+3*($5+4)/5-2)+1 )) set -- "$2" "$3" "$4" $(( $1/3600 )) $(( $1%3600 )) set -- "$1" "$2" "$3" "$4" $(( $5/60 )) $(( $5%60 )) printf "%04d-%02d-%02d %02d:%02d:%02d\n" "$@" ># Examples unixtime # => Current UNIXTIME date +%s # Linux command datetime2unixtime "2020-07-01 09:03:13" # => 1593594193 date -u +%s --date "2020-07-01 09:03:13" # Linux command unixtime2datetime "1593594193" # => 2020-07-01 09:03:13 date -u --date @1593594193 +"%Y-%m-%d %H:%M:%S" # Linux command
You must log in to answer this question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.