LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   generate dates between two dates (https://www.linuxquestions.org/questions/programming-9/generate-dates-between-two-dates-4175455044/)

ip_address 03-21-2013 04:01 PM

generate dates between two dates
 
Hi,

Is there a way to generate dates between two dates taking care of leap years? For example, generate all the dates between 2012-10-11 and 2014-05-01. Thanks!

Code:

2012-10-11
2012-10-12
2012-10-13
.
.
.
2012-10-30
2012-10-31
2012-11-01
.
.
.
2014-04-30
2014-05-01


OAnimal74 03-21-2013 04:07 PM

Of course there is. In which programming language?

ip_address 03-21-2013 04:12 PM

can we do it in bash / awk? Thanks!

OAnimal74 03-21-2013 04:19 PM

Not that familiar with those two languages unfortunately.

Guttorm 03-21-2013 04:28 PM

Code:

#!/bin/sh
d1=2012-10-11
d2=2014-05-01
while [ $d1 != $d2 ] ; do
        echo $d1
        d1=$(date -d "$d1 + 1 day" +%F)
done
echo $d2


Edit:
Should probably need some validation that the dates are proper, and that d2 is not before d1. And first I forgot to "echo $d2".


All times are GMT -5. The time now is 11:41 AM.