View Full Version : date range to month list

09-08-2010, 08:16 PM
This seems like it should be easy, but I keep making it hard. I have a date range which consists of a start date and end date. I want to evaluate an array of dates which consist of every 1st of the month just prior to and after the aforementioned date range. For example, if I have 6/23/10 to 8/3/10, I want to get an array that consists of 6/1/10, 7/1/10, 8/1/10, and 9/1/10.

Here's my complicated code:

dt_start = STR_TO_DT('06/23/10', DATE_FMT=1)
dt_stop = STR_TO_DT('08/03/10', DATE_FMT=1)
elapsed_days = DT_DURATION(dt_stop, dt_start) + 1
; Find previous 1st of Month
offset = dt_start.day - 1
dim = GREAT_INT((elapsed_days+offset+31)/28 + 1) > 2
dt_list = DTGEN(DT_SUBTRACT(dt_start, DAY=offset), dim, /MONTH)

This seems especially weak in the calculation of elapsed months (dim). Is there an easier, more robust way to do it? I keep think I'm missing the obvious.