- How can I get the current date and time in the terminal and set a custom command in the terminal for it? [closed]
- How to get the current Unix timestamp or epoch time from a Linux command line
- Echo Unix timestamp using the date command
- For example in an Ubuntu terminal/command line
- Storing timestamp in a bash script variable, and echo it
- What is epoch time (Unix timestamp)?
- The latest
- Bash-Basics
- Dates and Timestamps in UNIX/Linux
- Get the Current Date and Time in Linux
- UNIX Time
- Related Article — Linux Date
- How to display Unix time in the timestamp format?
- You must log in to answer this question.
- Related
- Hot Network Questions
- Subscribe to RSS
- Convert date time string to epoch in Bash
- 6 Answers 6
How can I get the current date and time in the terminal and set a custom command in the terminal for it? [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
I have to check the time in a Linux terminal. What is the command for getting date and time in a Linux terminal? Is there a way in which we can set a custom function?
This question seems on topic as it relates to a specific computing task on a specific operating system (which is just a software layer itself). As, @drstevens said, this is the first result in Google and it was informative for me needs. If it were off-topic, it would read something like «Do operating systems tell time?» or «What do you guys think about operating systems that tell time.» Stack Overflow is a great resource for beginners and it seems like this was closed as for being too beginner of a question. It should instead just be listed as a beginner question.
StackExchange is the place for answers. This is a good question. Perhaps the question should be migrated, but it should not be closed, in my opinion. This question appeared first in my Google results when I searched for «linux check time».
How to get the current Unix timestamp or epoch time from a Linux command line
Unix timestamp or epoch time is one of the most popular and easily generatable timestamp formats, quickly in a bash shell. Linux date command is a very powerful utility to get or set system time in various ways. Let us check, how to get Unix timestamp from a Docker container, Linux / Ubuntu terminal.
Echo Unix timestamp using the date command
By default, the Unix timestamp (epoch) is calculated in UTC, so there will be no difference in it regardless of the timezone.
For example in an Ubuntu terminal/command line
Storing timestamp in a bash script variable, and echo it
$ timestamp=$(date +%s) $ echo $timestamp 1262322992
What is epoch time (Unix timestamp)?
The Unix epoch is the time 00:00:00 UTC on 1 January 1970. The current epoch time is the number of seconds counted from there. Reference: https://en.wikipedia.org/wiki/Unix_time
The latest
Bash-Basics
- 40+ BASH commands for everyday use
- bash alias with arguments
- bash change case lower to upper
- bash check remote file exists
- bash date change from commandline
- bash date day before yesterday
- bash date day x days after
- bash date day x days ago
- bash date full date
- bash date iso 8601 format
- bash default value for variable
- bash delete tilde named directory
- bash env check
- bash if then else examples
- bash print date and time
- bash redirect stdout stderr separate files
- bash return code
- bash sed uncomment lines
- bash string remove spaces
- bash test command
- bash test symlink
- bash timestamp epoch time
- bash tr delete character(s)
- bash tree command not found
- bash until loop
- bash while loop
- bashrc alias
Dates and Timestamps in UNIX/Linux
- Get the Current Date and Time in Linux
- UNIX Time
Date and time information isn’t quite as visible on the command line as it would be with a graphical version of Linux, but we can certainly get that information with the date command.
This tutorial will explain how to use the date command to get, parse, convert dates and times, and print them in different formats on Linux, assuming a Bash shell command line.
Get the Current Date and Time in Linux
As shown, running date gives us the current date and time, with the time zone.
user@linux:~$ date Tue 01 Jan 2022 12:00:00 AM +04
The default date-time format will be in the time zone you are located in — in this case, the output of date is for a user whose time zone is Gulf Standard Time, or GMT+04 . To print out the date and time in a different format, such as UTC or GMT, or yyyy/mm/dd or dd/mm/yyyy , you can specify that format using special modifiers as follows.
# to print the current date in dd/mm/yyyy format date +%d-%m-%Y # to print the current date and time in UTC date -u # to print the date and/or time as per IETF RFC3349 date --rfc-3339=seconds # to print the date and time within a custom string date +"Today is %D and the time is %I:%M:%S"
UNIX Time
In most UNIX systems, the current time is stored as the time elapsed since a particular moment to simplify, keeping the time as a long integer, called the UNIX epoch. The universally accepted moment for all UNIX systems is January 1st 1970, 12:00:00 AM . This is called a UNIX timestamp and is recognized by all modern UNIX/Linux systems.
For example, if we wish to find the UNIX timestamp for 1st January 2022, we can use the date command.
user@linux:~$ date -d"1 January 2022 12:00 AM" +%s 1640980800
date attempts to parse the string for a formatted date and time (or, if a timestamp is not specified, assumes the time as 12:00 AM) and then prints out the UNIX timestamp form of the given date and/or time. 1640980800 is the exact number of seconds that have elapsed since January 1 1970, 12:00:00 AM.
The converse is also possible, whereby we take a UNIX timestamp and convert it to a date representation. To get back our original date, we can pass the UNIX timestamp as shown to convert it.
user@linux:~$ date -d @1640980800 Sat 01 Jan 2022 12:00:00 AM +04
If we wish, we can also include other parameters to format the date in a specific way, such as UTC or GMT, as explained above.
Related Article — Linux Date
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.
Convert date time string to epoch in Bash
The date time string is in the following format: 06/12/2012 07:21:22. How can I convert it to UNIX timestamp or epoch?
6 Answers 6
What you’re looking for is date —date=’06/12/2012 07:21:22′ +»%s» . Keep in mind that this assumes you’re using GNU coreutils, as both —date and the %s format string are GNU extensions. POSIX doesn’t specify either of those, so there is no portable way of making such conversion even on POSIX compliant systems.
Consult the appropriate manual page for other versions of date .
Note: bash —date and -d option expects the date in US or ISO8601 format, i.e. mm/dd/yyyy or yyyy-mm-dd , not in UK, EU, or any other format.
Note: if you want to specify the time in another timezone (like UTC) add -HHMM or +HHMM to the end. So date —date=’06/12/2012 07:21:22 -0000′ +»%s» converts UTC date to unix time stamp
For GNU date, if you wish to use UTC note that the format is yyyy-mm-dd hh:mm:ss have a look at gnu.org/software/coreutils/manual/html_node/… : gdate —date=’2019-06-18 00:02:00 +0000′ +%s
For Linux, run this command:
For macOS, run this command:
date -jf "%Y-%m-%d %H:%M:%S" "1970-01-01 00:00:00" +%s
On OSX the timestamp increases somehow with the current time. I currently don’t have an explanation for that.
Update: That was because it adjusts the timestamp for my localtime I guess. Adding a -u flag should fix that.
@AbdulRehmanJanjua The -u flag should come before the -f flag, or else the shell interprets it the format string. So, it should be: date -j -u -f «%a. «
A lot of these answers overly complicated and also missing how to use variables. This is how you would do it more simply on standard Linux system (as previously mentioned the date command would have to be adjusted for Mac Users) :
#!/bin/bash orig="Apr 28 07:50:01" epoch=$(date -d "$" +"%s") epoch_to_date=$(date -d @$epoch +%Y%m%d_%H%M%S) echo "RESULTS:" echo "original = $orig" echo "epoch conv = $epoch" echo "epoch to human readable time stamp = $epoch_to_date"
RESULTS: original = Apr 28 07:50:01 epoch conv = 1524916201 epoch to human readable time stamp = 20180428_075001
# -- Converts from human to epoch or epoch to human, specifically "Apr 28 07:50:01" human. # typeset now=$(date +"%s") # typeset now_human_date=$(convert_cron_time "human" "$now") function convert_cron_time() < case "$" in epoch) # human to epoch (eg. "Apr 28 07:50:01" to 1524916201) echo $(date -d "$" +"%s") ;; human) # epoch to human (eg. 1524916201 to "Apr 28 07:50:01") echo $(date -d "@$" +"%b %d %H:%M:%S") ;; esac >