问题
In my script I'm storing the current in date in a variable, however I would like the variable to store the date 5 minutes ahead.
29/10/2014-10:47:06 to 29/10/2014-10:52:06
Is there a quick and easy way to do this in Bash? Looking for a solution that would also work correctly if the time was 10:59:00 for example (recognising to enter the next hour).
I've found some Perl solutions but nothing for Bash.
回答1:
You can use the -d
option:
$ date
mercredi 29 octobre 2014, 11:45:45 (UTC+0100)
$ date -d '5 mins'
mercredi 29 octobre 2014, 11:50:55 (UTC+0100)
And if you want to use a variable:
$ curr=$(date +%d/%m/%Y-%H:%M:%S)
$ echo $curr
29/10/2014-11:59:50
$ date -d "($cur) +5mins" +%d/%m/%Y-%H:%M:%S
29/10/2014-12:04:54
回答2:
you need to use -d option, with any user defined date:
date -d '2014/10/29 10:58:06 5 minutes'
Wed Oct 29 11:03:06 IST 2014
for current date
date -d '5 minutes'
回答3:
Have a look at date
:
date +%s
gives you the seconds since 1.1.1970, add 5*60 and convert it back using
date --date='@<newTime>'
.
See man date
in bash.
回答4:
In Bash, the following options are supported for date
command:
-a [-]sss.fff Slowly adjust the time by sss.fff seconds
(fff represents fractions of a second). This
adjustment can be positive or negative. The
system's clock is sped up or slowed down
until it has drifted by the number of
seconds specified. Only the super-user may
adjust the time.
So, use the following to add 5 mins (=300 seconds):
date -a 300
来源:https://stackoverflow.com/questions/26628392/incrementing-current-date-by-5-minutes-in-bash