Skip to content
Advertisement

Bash list datetime between two dates

How to list all datetimes in the format "+%Y-%m-%d %H:%M:%S" ?

This only prints the dates, but I want the time included as well i.e. 2016-07-20 08:33:21

start=2013-09-05 
end=2013-09-11

   while [[ $start < $end ]] 
     do 
      printf "$startn"; start=$(date -d "$start + 1 day" +"%Y-%m-%d") 
     done

Advertisement

Answer

Hope this one can help you:

#!/bin/bash

start="2016-07-20 08:33:50"
end="2016-07-20 08:34:01"

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

Output:

2016-07-20 08:33:50
2016-07-20 08:33:51
2016-07-20 08:33:52
2016-07-20 08:33:53
2016-07-20 08:33:54
2016-07-20 08:33:55
2016-07-20 08:33:56
2016-07-20 08:33:57
2016-07-20 08:33:58
2016-07-20 08:33:59
2016-07-20 08:34:00
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement