在 Linux 中打印日期范围的日期

14

我对Linux不熟悉。如何在给定日期范围内打印和存储日期。

例如,假设startdate=2013-03-01并且enddate=2013-03-25;我想打印出该范围内的所有日期。

提前感谢。

7个回答

20
只要日期是以YYYY-MM-DD格式存储的,您就可以按字典序比较它们,并且不需要首先将其转换为秒即可让date执行日历算术:
startdate=2013-03-15
enddate=2013-04-14

curr="$startdate"
while true; do
    echo "$curr"
    [ "$curr" \< "$enddate" ] || break
    curr=$( date +%Y-%m-%d --date "$curr +1 day" )
done

使用 [ ... ] 时,您需要转义 < 来避免与输入重定向运算符混淆。

如果开始日期大于结束日期,则会出现打印开始日期的问题。


所以,在测试后只需执行 echo "$curr"date --date "$curr +1 day"let cur=.. 更好。 - xtof pernod
最好将此放入for循环而不是while循环中,以确保安全。如果在脚本中参数化您的startdate和enddate,并使循环运行最多366次或1000次,则可以避免无限循环。 - ekangas

9
如果你想要“最近”的日期,可以使用以下替代方法:
echo {100..1} | xargs -I{} -d ' ' date --date={}' days ago' +"%Y-%m-%d"

显然,这种方法无法处理任意日期范围。

这是我认为最接近最佳答案的内容,我对其进行了更改:$ startdate='2016-03-01'; echo {0..10} | xargs -I{} -d ' ' date --date="$startdate +"{}" days" +"%Y-%m-%d" 2016-03-01 2016-03-02 2016-03-03 .... - user5672998

9

另一个选项是使用来自dateutilshttp://www.fresse.org/dateutils/#dateseq)的dateseq

$ dateseq 2013-03-01 2013-03-25
2013-03-01
2013-03-02
2013-03-03
2013-03-04
2013-03-05
2013-03-06
2013-03-07
2013-03-08
2013-03-09
2013-03-10
2013-03-11
2013-03-12
2013-03-13
2013-03-14
2013-03-15
2013-03-16
2013-03-17
2013-03-18
2013-03-19
2013-03-20
2013-03-21
2013-03-22
2013-03-23
2013-03-24
2013-03-25

不错!在 Vim 中::read ! dateseq 2018-12-04 2019-12-31:read 将输出附加到当前缓冲区(文件);! 执行外部(BASH)命令(dateseq ...)。 - Victoria Stuart
这对我来说非常重要。谢谢你。 - Barett

3
使用 date 将日期转换为秒,进行一些数学运算后再将其转换回去即可:
#/bin/bash

dstart=2013-03-01
dend=2013-03-25
# convert in seconds sinch the epoch:
start=$(date -d$dstart +%s)
end=$(date -d$dend +%s)
cur=$start

while [ $cur -le $end ]; do
    # convert seconds to date:
    date -d@$cur +%Y-%m-%d
    let cur+=24*60*60
done

更多关于日期参数的信息,请参阅man date


1
略微改进的版本
#!/bin/bash
startdate=2013-03-15
enddate=2013-04-14

curr="$startdate"
while true; do
    [ "$curr" \< "$enddate" ] || { echo "$curr"; break; }
    echo "$curr"
    curr=$( date +%Y-%m-%d --date "$curr +1 day" )
done

1

一行版本:

seq 0 24 | xargs -I {} date  +"%Y-%m-%d" -d '20130301 {}day'

# this version is ok if the dates not cross next month
seq -f'%.f'  20130301 20130325

3
请提供更多详细信息以扩展您的答案,例如工作代码或文档引用。 - Community

0
一个简单的演示
start_date="20191021"                                                                        
end_date="20191025"                                                                          
dates=()                                                                                     
while [[ "${start_date}" != "${end_date}" ]];do                                              
  formatted_date=$(date -d "${start_date}" +"%Y%m%d")                                        
  dates+=( "${formatted_date}" )                                                                                                                                
  start_date=$(date -d "$start_date +1 day" +"%Y%m%d")                                       
done 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接