0

I want to write a script that will display the time per minute between two date and time. The Start and End time gap can be anything, could be 5minutes, 3hours, 30days, etc.

Example:

Start time: 2019-12-31 23:57:00
End Time  : 2020-01-01 00:03:00

Result:

 2019-12-31 23:57:00
 2019-12-31 23:58:00
 2019-12-31 23:59:00
 2020-01-01 00:00:00
 2020-01-01 00:01:00
 2020-01-01 00:02:00
 2020-01-01 00:03:00

Edit: Im getting the error below when using the -d option.

     date: Not a recognized flag: d
     Usage: date [-u] [+"Field Descriptors"]
0

With GNU date that can handle expressions like +1 minute, it's pretty easy:

#!/bin/bash
start='2019-12-31 23:57:00'
end='2020-01-01 00:03:00'

date=$start
while [[ $date < $end || $date == $end ]] ; do
    echo "$date"
    date=$(date -d "+1 minute $date" '+%Y-%m-%d %H:%M:%S')
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.