Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incrementing current date by 5 minutes in bash

Tags:

date

bash

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.

like image 312
user3722194 Avatar asked Jan 09 '23 13:01

user3722194


2 Answers

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
like image 98
fredtantini Avatar answered Jan 17 '23 19:01

fredtantini


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'
like image 24
Hackaholic Avatar answered Jan 17 '23 19:01

Hackaholic