in shell, when I print this
date -d "2016-11-23 13:05 -1 hours " "+%Y-%m-%d %H:00:00"
I get 2016-11-23 23:00:00.Strange!
when I print this
date -d "2016-11-23 13:05 1 hours ago" "+%Y-%m-%d %H:00:00"
I get 2016-11-23 12:00:00.
Why they are different? What I think is that they are both 2016-11-23 12:00:00.
This is because the negative number is treated as an offset to your timezone, not to the 13:05. In my timezone, MET (one hour east of GMT), this is what I get:
$ date -d "2016-11-23 13:05 -1 hours " "+%Y-%m-%d %H:00:00"
2016-11-23 16:00:00
$ TZ=GMT date -d "2016-11-23 13:05 -1 hours " "+%Y-%m-%d %H:00:00"
2016-11-23 15:00:00
$ TZ=GMT-1 date -d "2016-11-23 13:05 -1 hours " "+%Y-%m-%d %H:00:00"
2016-11-23 16:00:00
$ TZ=GMT-1 date -d "2016-11-23 13:05 -2 hours " "+%Y-%m-%d %H:00:00"
2016-11-23 17:00:00
The timezone offset is usually specified as a four digit number, as in
Sun, 29 Feb 2004 16:21:42 -0800
but apparently date(1) is happy with a -1 as well.
From the man page:
DATE STRING
The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb
2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". A date string may
contain items indicating calendar date, time of day, time zone, day of week, relative
time, relative date, and numbers. An empty string indicates the beginning of the day.
The date string format is more complex than is easily documented here but is fully
described in the info documentation.
Related
I need to find a time X hours before the previous midnight, thus I would like to subtract X hours from a date object.
Example
# Finding the previous midnight
date -d "yesterday 23:59:59"
Mon Jul 11 00:00:00 CEST 2022
What I want
I would like to find the date X hours before this midnight
x=4
Mon Jul 10 20:00:00 CEST 2022
You can use this date command:
date -d "today 0 -4 hours"
Here:
today 0: gets midnight date-time for today's date
-4 hours: subtracts 4 hours from midnight time
It seems one can just write the following:
date --date 'yesterday 23:59:59 CEST -4 hours'
I have this data:
`date +%Y-%m-%d`" 00:00:00"
that return 2015-10-08 00:00:00
I would like cancel 5 minute:
2015-10-07 23:55:00
Many thanks
You need to subtract 5 minutes from a known point in time:
$ date -d "00:00:00 today"
Thu Oct 8 00:00:00 EDT 2015
$ date -d "00:00:00 today -5 minutes"
Wed Oct 7 23:55:00 EDT 2015
You just need to add your format string.
There's more than one way to subtract a value from the current time, although this should match the format shown in your question:
date -d "-5 min" "+%Y-%m-%d %H:%M:%S"
Result:
2015-10-08 15:26:13
I want to find a date that is 57 –working– days after an arbitrary date using date or bash. For instance,
today is august 21st,
reference date is july 15th,
what days will be 57 working days after july 15th?
This should work to just get all days
date -d '7/15/14 +57 days'
To get number of work days (M..F) you can do something lazy like this
#!/bin/bash
days=0
for ((i=1;i>0;i++)) do
future=$(date -d "7/15/14 +$i days" '+%w')
((future!=0 && future!=6)) && ((days++)) # ignore sunday (0) and saturday (6)
((days==57)) && date -d "7/15/14 +$i days" && break
done
e.g.
> ./abovescript
> Thu Oct 2 00:00:00 CDT 2014
Weird solution:
day=21
mon=8
year=2014
days=4
curl -s "http://www.timeanddate.com/scripts/dateserver.php?mode=addweekdays&d1=$day&m1=$mon&y1=$year&type=add&ad=$days&atyp=0&ach=3" | sed -n 's|.*<h2>Result: \(.*\)</h2>.*|\1|p'
prints
Wednesday, August 27, 2014
the date after 4 working days from 2014.08.21
for the
day=15
mon=7
year=2014
days=57
prints
Friday, October 3, 2014
This question already has answers here:
Using `date` command to get previous, current and next month
(5 answers)
Closed 8 years ago.
I have been using the command:
date --date='1 months ago' +%b
To get the month name of the month it was a month ago, but have realised today as it is the 31st that this command actually gives me the month name it was 4 weeks ago.
Is there any way to get the calendar month that it was 1 month ago, or indeed n months ago as I can see that the discrepancy will be greater as the number of months is longer.
Date calculations that depend on the number of days in the month are tricky. A hybrid approach, using month numbers and a lookup table, will probably work best.
months=("" Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
echo ${months[$(date +%m) - 1 ]}
[[ $(date +%d) == "31" ]] && date -d'-31 day' +%b || date -d'-1 month' +%b
test with today:
kent$ date
Thu Jul 31 17:34:27 CEST 2014
kent$ [[ $(date +%d) == "31" ]] && date -d'-31 day' +%b || date -d'-1 month' +%b
Jun
try this one line
#if the month before 30 days is the same of the actual month ,then return the month before 31 days
[[ `date --date='30 day ago' +%b` == `date +%b` ]] && echo `date --date='31 day ago' +%b` || echo `date --date='30 day ago' +%b`
i have search for a solution to my shell date subtraction issue with no joy so here goes.
i have a date format like so %m%d%H%M%S which is "0102231203" and the second %Y%m%d%H%M%S, i can take the year off the second one and do a normal subtraction but when it is over a day it becomes an issue with the time being incorrect.
here is what i have tried so far
BTT=0102234500
TPP=0102233635 (after removing the year)
BT=date -d ${BTT}
TP=date -d ${TPP}
and
BT=date -d $BTT +%m%d%H%M%S
TP=date +%m%d%H%M%S -d ${TPP}
date: invalid date `0102234500'
date: invalid date `0102233635'
BT=date -d #${BTT} +%m%d%H%M%S
TP=date +%m%d%H%M%S -d #${TPP}
weird output
0329071355
0329072820
BT=date -d #${BTT}
TP=date -d #${TPP}
Thu Mar 29 07:13:55 BST 1973
Thu Mar 29 07:28:20 BST 1973
even changed it to add the year to both still
BTT=20130102234500
TPP=20130102233635
BT=date -d #${BTT}
TP=date -d #${TPP}
Fri Jul 19 08:53:55 GMT 639867
Fri Jul 19 09:08:20 GMT 639867
how do i resolve this issue.
tnx
The -d option of date accept human readable string so if you can have full length date you can do :
me#server:/tmp$ BTT=`date +"%Y-%m-%d %H:%M:%S"`
me#server:/tmp$ TPP=`date +"%Y-%m-%d %H:%M:%S"`
me#server:/tmp$ echo $((`date -d "$TPP" +%s`-`date -d "$BTT" +%s`))
3
With your datas :
me#server:/tmp$ BTT="2013-01-02 23:45:00"
me#server:/tmp$ TPP="2013-01-02 23:36:35"
me#server:/tmp$ echo $((`date -d "$BTT" +%s`-`date -d "$TPP" +%s`))
505
With the results in seconds.