In linux, date
can help me to print the current time. If want to print the current time + 1 hour, what option should I give?
According this source you can just do:
date --date='1 hour'
In Linux shell script to add 1 hour or 60 minutes.
dt=$(date -d '+ 60 minutes' '+%FT%T.000Z')
TZ=Asia/Calcutta date
should work (obviously this will get you the time in Calcutta).
This changes your TimeZone (TZ
) environment variable for this operation only, it won't permanently set you to the new time zone you specify.
We can get it using below code, i was handling a case wherein i required to traverse dates from last 3 months, i used below code to traverse the same
for i in {90..1}
do
st_dt=$(date +%F' 00:00:00' -d "-$i days")
echo $st_dt
j=$((i-1))
end_dt=$(date +%F' 00:00:00' -d "-$j days")
echo $end_dt
done
Just use -d
(or --date
) to do some math with the dates:
date -d '+1 hour' '+%F %T'
# ^^^^^^^^^^^^
For example:
$ date '+%F %T'
2013-04-22 10:57:24
$ date -d '+1 hour' '+%F %T'
2013-04-22 11:57:24
# ^
Warning, the above only works on Linux, not on Mac OS.
On Mac OS, the equivalent command is
date -v+1H
In shell script, if we need to add time then use below command and date format(PUT TIME before DATE string)
date -d"11:15:10 2017-02-05 +2 hours" +"%Y-%m-%d %H:%M:%S"
this will output
2017-02-05 13:15:10
THis does not result in correct date without UTC it does not work