I need to get the list of dates between two dates – for e.g. I have start date 03302012 and enddate 05302012 and I need the output like
03302012 03312012 04012012 04022012 ... 05282012 05292012 05302012
- Feel free to use your date format – I will make transformation by myself.
Thank you very much!
You could do this by using a recursive template. It’s not the greatest solution since this is subject to stack overflow or out of memory errors (if the date interval you give it is large enough), but it can be a place to start.
First thing you need for the example below is a way to check a generated value and make sure it’s a valid date (you can find some examples online for that; I just used a sample from an open source project and imported the file in the example just to keep it shorter).
Then, the idea is to generate the dates by adding a day to the previous one, and then a day to the current one on so on. If the day overflows, you add one to the month and start again from day 1. If month overflows you do the same with the year and start from month 1. Theoretically the only one that never overflows is the year (but you will limit that with the end value of the interval).
You generate a date and you validate it. If it’s valid and haven’t reached the end of the interval you try to generate another by adding days to it (that’s what happens in block X).
When you get an invalid value, then something overflowed. It can only be the day or the month (as I mentioned above). I first check for the month (block Y). If that’s the case I start from day 1 of month 1 but for the next year. If it’s the day that overflowed (block Z), then I start from day 1 of the next month.