当前位置: 动力学知识库 > 问答 > 编程问答 >

linux - 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 "$start\n"; start=$(date -d "$start + 1 day" +"%Y-%m-%d")

done

网友答案:

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
网友答案:

You could try this;

#!/bin/bash
start=2013-09-05 
end=2013-09-11

while [[ $start < $end ]] 
 do 
   start=$(date -d "$start + day + minute" +"%Y-%m-%d %H:%M:%S") 
   printf "$start\n"; 
 done
分享给朋友:
您可能感兴趣的文章:
随机阅读: