I'm trying to calculate the number of seconds since the Epoch, using date on MacOS BSD.
I can get a one year ago date string:
$ date -v -1y
Tue Apr 21 10:44:47 EST 2020
...but I can't figure out how to convert it into seconds since Epoch. Any suggestions?
Add +%s to tell it to print the datetime as seconds since the epoch:
date -v -1y +%s
The + is a date option to set the output format, and %s is strftime format for "seconds since epoch".
Portability note: while the +%s part is pretty standard and portable (though the %s format is not actually required by POSIX), the -v -1y part is wildly nonportable. With GNU date (e.g. on most Linuxes), you'd use something like --date='1 year ago' instead. On NetBSD, -d '1 year ago' works. Check your local man page to see what your system supports.
Related
What I intent to get is
$ xxx 2019-10-11 <= insert your command
1570752000
The output is timestamp in Oct 11 00:00:00 UTC 2019. I find a good way to do this in gnu, but not in bsd
This should work:
date -j -f '%F %T %Z' '2019-10-11 00:00:00 U' '+%s'
-j is for dry-run; i.e it prevents date from changing system date and time,
-f is for specifying input format,
+%s is for converting given date to seconds since Epoch.
On NetBSD the following will work:
TZ=GMT0 date -d '2019-10-11 00:00:00' '+%s'
Note the use of the TZ environment variable to specify the input timezone instead of trying to have it parsed from the input (though it may be possible to have a more properly formatted timezone parsed from the input, though then that leaves the question of what timezone the output should be formatted in).
On MacOS you might try something similar to what Oguz suggested:
TZ=GMT0 date -j -f '%Y-%m-%d %H:%M:%S' '2019-10-11 00:00:00' '+%s'
I'm having trouble with checking time since EPOCH. (and late subtract it from another one).
I get the date like this:
var=$(date)
echo $var
wto, 1 mar 2016, 16:00:14 CET
and later I'm trying to turn it into seconds since epoch:
date -d "$var" +"%s"
date: invalid date ‘wto, 1 mar 2016, 16:00:14 CET’
I'm giving this just as an example. Actually I will be reading the date from file, written in default locale format (I'm operating on couple different machines).
if you type date -h there is the reason why you got this error.
the -d option MUST be declared only with TIME and not with complete DATE format
-d,--date TIME Display TIME, not 'now'
so
date -d "23:59:59"
then:
Tue Mar 1 23:59:59 2016
if you need get only the seconds from a date you have to execute this:
date +"%S"
if you use the -d the output will be deplyed in msec
Need a help in unix shell script in calculating date.
I will be getting date value (eg: 20150908) as parameter, now inside the script i need to calculate 7 days ago date (20150908 -7).
something like below:
date=20150908
lastweek_date=20150908 - 7 ---> this should output as 20150901
Could someone help me on this.
Thanks
With GNU date, we can subtract one week:
$ date -d "20150908 - 1 week" '+%Y%m%d'
20150901
Alternatively, we could subtract 7 days:
$ date -d "20150908 - 7 days" '+%Y%m%d'
20150901
And, to show that this works over month boundaries:
$ date -d "20150901 - 1 week" '+%Y%m%d'
20150825
This solution is not OSX/BSD compatible.
A week is 604800 seconds long so to get the number of seconds since the epoch in a portable and POSIX compliant fashion and use it to compute the date 1 week ago do as follows:
PRESENT=$( date +%s )
WEEKAGO=$(( PRESENT - 604800 ))
printf "%s\n" "$WEEKAGO"
If i give the date "20130828"(not a current date) in YYYYMMDD format, how can i get 10 days back date using shell script i.e. 20130818
Thanks in advance!
Try
date +%Y%m%d --date="20130818 -10 day"
or even
date +%Y%m%d --date="20130818 10 days ago"
+%Y%m%d is the format of your date (YYYYmmdd), and through --date you can provide a string (in a very human readable format) to specify when you want this date.
The following:
date --date="20130828 - 10 days" +"%Y%m%d"
Outputs
20130818
if you have gnu date, you can just give 10 days ago to date's -d option:
kent$ date -d'10 days ago 20130828' +%Y%m%d
20130818
At work all the days config files are generated fresh and appended with a
session number. The company went public on Feb 16, and the 86400 is seconds
in one day. The session number is generated by subtracting the company start
day from seconds_since_last_day and adding a few zero's
That is the key to interacting with the days config files. I get this - However I do not
understand the
date -ud "$distance days ago 00:00:00".
Is it the number of seconds since 1970?
if $session; then
# return the session of the last day
seconds_since_day_one=`date -ud "Feb 16 2002" +"%s"`
seconds_since_last_day=`date -ud "$distance days ago 00:00:00" +"%s"`
days_between=`printf "%010d" $(( (seconds_since_last_day - seconds_since_day_one) / 86400 ))`
# Truncate on the left to 9 bytes to leave room
# to append the engine suffix for your environment
echo $days_between | awk '{l=length($1); print substr( $1, (l-8), l )}'
date -ud "$distance days ago 00:00:00" in itself just prints the date a certain amount of days ago in a quite readable format, but when you add the FORMAT string to control the output +"%s" does indeed mean the number in so called Unix Time (number of seconds since 1970-01-01 00:00:00 UTC).
If the variable $distance is set to a number it shows the date that number of days ago, if its set to 0 it means today, 1 it means yesterday, 2 the day before yesterday and so on. To better understand these formats and relative keywords there are rather good documentations in (amongst other places) the GNU coreutils package.
Check these URLs:
http://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html#Relative-items-in-date-strings
http://www.gnu.org/software/coreutils/manual/html_node/Date-input-formats.html
http://www.gnu.org/software/coreutils/manual/html_node/date-invocation.html#date-invocation
Wikipedia explanation of Unix Time:
http://en.wikipedia.org/wiki/Unix_time
The option -d to date provides a generic string to obtain the date.
So, for example, date -d yesterday will print yesterday's date, and date -d 'yesterday 12:00 AM' will print yesterday's date with the time set to 12:00 AM.
So, date -d 6 days ago 00:00:00 will print the date from 6 days ago, with the time set to 00:00:00. I hope it answers your question.
The format +"%s" tells date to print the number of seconds from 1970, instead the date.
mktime and strftime in awk can be used to get the date of the time.
http://www.gnu.org/software/gawk/manual/html_node/Time-Functions.html
For instance, strftime("%A",mktime("YYYY MM DD 00 00 00"))
should give you the day.