Suppose todays' date is formatted to YYYYMMDDHHMMSS such as 20160720152654. I would want to add hours or minutes or seconds to date.
Add 1 hour should change date to 20160720162654 Add 1 minute should change date to 20160720152754 Add 1 second should change date to 20160720152655
This seems to give me incorrect results
d=date +%Y%m%d%H%M%S
pd=$(($d + ($d % (15 * 60))))
echo $d
echo $pd
Output 20160720155141 20160720155482
You can manipulate input to pass it to
date -d
:Minor addition to anubhava's answer:
Since the timezone info is not available in YYYYMMDDHHMMSS, you may get unexpected results depending on your actual timezone, such as:
As far as I could understand, this happens because of the
+1 minute
expression, which causesdate
to ignore your current timezone:In my case, the timezone offset was +3, so that was causing problems:
You should be able to make it work on all timezones, by adding the current timezone to the "-d" parameter:
Note 1 : All above commands are run on RHEL 7.4 & GNU bash, version 4.2.46(2)-release
Note 2 : I am sure there must be an easier way :)