8

I need to generate strings with all days in a year

ex:

MIN_DATE=01.01.2012 MAX_DATE=31.12.2012 for date in {1...366..1} do echo ... done 

3 Answers 3

12
for d in {0..365}; do date -d "2012-01-01 + $d days" +'%d.%m.%Y'; done 
Sign up to request clarification or add additional context in comments.

5 Comments

how to do this automatically with leak years?
@DmitryDubovitsky What do you mean? 29.02.2012 is present in the output.
Want to output it automatically, for example For 2011 year last record would be 2012-01-01 something that: days_in_year=date "2012-12-31" +"%j"; for d in in ...
@DmitryDubovitsky Ah, I get it now. Well, I'd just do: for d in {0..365}; do date -d "$year-01-01 + $d days" +'%d.%m.%Y'; done | grep $year. That'll work whether year is 2011 or 2012.
This is not working in OS X since it uses a different version of date.
2

Not a pure bash solution, but my dateutils can help:

dseq 01.01.2012 31.12.2012 -f %d.%m.%Y -i %d.%m.%Y => 01.01.2012 02.01.2012 ... 31.12.2012 

Output format can be configured with -f and input format with -i.

Comments

1

Using an ISO 8601 date format (year-month-day), you can compare dates lexicographically. It's a little messier than I'd like, since bash doesn't have a "<=" operator for strings.

year=2011 d="$year-01-01" last="$(($year+1))-01-01" while [[ $d < $last ]]; do echo $d d=$(date +%F --date "$d + 1 day") done 

2 Comments

Comparing years lexicographically is a bad idea if your years can have a different number of digits. For example d="999-01-01"; last="1000-01-01" will produce no output.
ISO-8601 requires at least 4 digits for the year (although the problem will arise if you are trying to avoid a Y10k problem).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.