I need to like date
the same things simple, but since 1970 the number of seconds, instead of the current date, hours, minutes and seconds.
date
It does not seem to offer this option. There is a simple way to do this?
#1st Floor
This should work:
date +%s
#2nd Floor
Just add something.
Gets the number of seconds of any particular date (for example, October 21, 1973) from epoch (January 1, 1970) since.
date -d "Oct 21 1973" +%s
The number of seconds back to a date
date --date @120024000
Command date
is very flexible. You can use the date to make another cool thing (from date --help
shamelessly copy date --help
). Friday 9:00 the next show in the US West Coast
date --date='TZ="America/Los_Angeles" 09:00 next Fri'
Better yet, take the time to read the manual page http://man7.org/linux/man-pages/man1/date.1.html
#3rd floor
This is an extension done @pellucide, but for Macs:
Since determine any specific date from epoch (January 1, 1970) (for example, October 21, 1973) the number of seconds
$ date -j -f "%b %d %Y %T" "Oct 21 1973 00:00:00" "+%s"
120034800
Note that for the sake of completeness, I have time to add a section format. The reason is that date
the use of any part of your date given, and the current add time to the value provided. For example, if you execute the above command in 4:19 in the afternoon, but no '00: 00:00 'section, it will automatically add the time. Such "21 October 1973" will be resolved as "October 21 1973 16:19:00." This may not be what you want.
To convert back to a time stamp date:
$ date -j -r 120034800
Sun Oct 21 00:00:00 PDT 1973
Apple's realization date manual page: HTTPS : //developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/date.1.html
#4th floor
So far, all the answers are using an external program date
.
From the Bash 4.2 starts, printf
a new modifier %(dateformat)T
, when the parameter -1
is used together, it outputs the current date, the format dateformat
is given, by the strftime(3)
processing ( man 3 strftime
information format).
So, for pure Bash solution:
printf '%(%s)T\n' -1
Or if you need to store the result in a variable var
:
printf -v var '%(%s)T' -1
No external program, no sub-shell!
Starting Bash 4.3, you can not even specify -1
:
printf -v var '%(%s)T'
(But always to the argument -1
might be more sensible).
If using -2
as a parameter instead -1
, Bash will use the time to shell rather than the current start date (but why?).
#5th Floor
Using this bash script (I ~/bin/epoch
):
#!/bin/bash
# get seconds since epoch
test "x$1" == x && date +%s && exit 0
# or convert epoch seconds to date format (see "man date" for options)
EPOCH="$1"
shift
date -d @"$EPOCH" "$@"
#6th floor
Most Awk implementation:
awk 'BEGIN {srand(); print srand()}'
#7th floor
Pure bash solution
From bash
5.0 ( January 7, 2019 released since), you can use the built-in variables EPOCHSECONDS
.
$ echo $EPOCHSECONDS
1547624774
There EPOCHREALTIME
, including a few seconds.
$ echo $EPOCHREALTIME
1547624774.371215
By removing the decimal point can be EPOCHREALTIME
converted to microseconds (μs). When using bash
the built-in algorithm (( expression ))
, it might make sense, it can only handle integers.
$ echo ${EPOCHREALTIME/./}
1547624774371215
In all examples above, the printing time is equal to the value of better readability. In fact, the time value will be different, because each command takes very little time to perform.