Script shell how to add x minutes to a given date? - bash

I need to add 10 minutes to a given date :
givenDate = 2016-10-25 18:22:37
when executing :
newDate=$(date +'%Y-%m-%d %T' --date="$givenDate + 10 minutes")
echo $newDate
I get :
2016-10-25 00:10:00
instead of
2016-10-25 18:32:37
2nd question: How can I round the minutes number so I can get these results per exemple:
18:08 -> 18:10
18:32 -> 18:40
18:46 -> 18:50
18:55 -> 19:00
Thank you.

for the first question drop the + like this:
date +'%Y-%m-%d %T' --date="$givenDate 10 minutes"
for the second question we have to extract the last digit of current minutes, then compute the number of minutes to add to make it round using modulo 5:
givenDate="2016-10-25 18:22:37"
minute=$(echo $givenDate | sed 's/.*\([0-9]\):..$/\1/')
rounder=$((5 - minute % 5))
date +'%Y-%m-%d %T' --date="$givenDate $rounder minutes"
note that the seconds haven't been taken into account

Related

Get saturday date for the given input date using bash script

By using the below command, it will return the last saturday date.
date +"%b-%d-%Y" -d "last saturday"
Sep-01-2018
I want to pass input date as parameter, which should return the last saturday's date in bash script.
Aug-08-2018 -----> Aug-04-2018
Jun-04-2018 -----> Jun-02-2018
Get a negative number that will be the number of days to subtract. We use 13, because Saturday is 6, and 6 + 7 = 13. This will get us the Saturday one or two weeks ahead. Then we modulo 7, to ensure it is NEXT Saturday, then subtract 7 to make it LAST Saturday. Then we put that diff into the date string:
$ date_str="Aug-08-2018"
$ diff=$(( (13 - $(date +"%u" -d ${date_str})) % 7 - 7))
$ date -d "${date_str} ${diff} days"
Sat Aug 4 00:00:00 EDT 2018

How to subtract a time from date time format in shell? [duplicate]

This question already has answers here:
How to subtract 5 minute from date
(2 answers)
Closed 4 years ago.
I have a particular date and time with me in shell. Now I need to subtract 7 hours from that particular date and time.
eg.
2018-03-20 21:00:00 -> 2018-03-20 14:00:00
2018-03-20 06:00:00 -> 2018-03-19 23:00:00
I have both date and time in different strings as well.
How to write this in shell (v4.1)?
Some implementations of date understand time arithmetics like "-7 hours":
#!/bin/bash
format='+%Y-%m-%d %H:%M:%S'
Test () {
date=$1
time=$2
expect=$3
d=$(date "$format %Z" -d "$date $time")
plus7=$(date "$format" -d "$d -7 hours")
if [[ $plus7 == $expect ]] ; then
echo ok
else
echo $plus7
fi
}
Test 2018-03-20 21:00:00 '2018-03-20 14:00:00'
Test 2018-03-20 06:00:00 '2018-03-19 23:00:00'
Tested with date (GNU coreutils) 8.25.
You can use the POSIX arithmetic operator in conjunction with date -d# to subtract 7 hours (7 * 3600 seconds) from any given date, e.g.
$ date -d#$(($(date -d"2018-03-20 21:00:00" +%s) - 7 * 3600))
Tue Mar 20 14:00:00 CDT 2018

Determining week number on a 4 week cycle in bash script

what I want is a cycle of 4 weeks in a bash script
My question is: How do I know this week's number in the cycle.
week x monday : echo one
week x+1 monday : echo two
week x+2 monday : echo three
week x+3 monday : echo four
and again
week x+4 monday : echo one
and so on
what I have is the epoch
(UTC), Thursday, 1 January 1970
consequently
(UTC), monday, 5 January 1970 (I can set this to echo 1)
Any suggestions? Converting dates is no problem. Just a general idea is ok.
I think you are expecting do something like this, with GNU date,
start_date=$(date -d "1970-01-05" '+%s') # Corresponding to 1
end_date=$(date -d "2017-01-02" '+%s') # Current week
Number of weeks between the dates
numberOfWeeks=$(( ( end_date - start_date )/(60*60*24*7) ))
printf "%s\n" "$numberOfWeeks"
2452
Now to determine which week this corresponds to, do
printf "The current week %s belongs to week %d" "$(date)" "$(((numberOfWeeks%4) + 1))"
The current week Mon, Jan 02, 2017 4:47:09 PM belongs to week 1
For further weeks down the line, say. 4th Monday of March 2017, using the above computation, i.e. with
end_date=$(date -d "2017-03-27" '+%s')
printf "The week %s belongs to week %d" "$(date -d "2017-03-27")" "$(((numberOfWeeks%4) + 1))"
The week Mon, Mar 27, 2017 12:00:00 AM belongs to week 1
Another example for the 3rd Monday or March 2017,
end_date=$(date -d "2017-03-20" '+%s')
printf "The week %s belongs to week %d" "$(date -d "2017-03-20")" "$(((numberOfWeeks%4) + 1))"
The week Mon, Mar 20, 2017 12:00:00 AM belongs to week 4
You can format the date output for showing the week number:
function printweek {
weeknr=$(date '+%V' -d "+$1 weeks")
echo "$((weeknr%4))"
}
# Test
for week in 0 1 2 3 4 5 6 30 31 32 33; do
echo "Week offset from today ${week} => $(printweek ${week})"
done
This will work when you start over counting each year (first week 1 again). When you want to continue counting on 1 Januari, the script will be more difficult. You can look at the solution of #Inian.
Another option might be looking at the output of the last run, and add one %4 to the weeknumber of the last run.

shell date "-n hours" differs with "n hours ago" in some situation

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.

How to subtract 5 minute from date

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

Resources