e false endend## Compute the first and last dates according to the params.## @param first [String] [First date]# @param last [String] [Last date]## @return [Array] [Array include first and last date]def generate_date_range(first, last) first, last = "", first unless last if last.nil? || last.empty? last = (Time.now - 1.day).in_time_zone('Kolkata').strftime("%Y-%m-%d") end if first.empty? first = Time.strptime(last, "%Y-%m-%d").in_time_zone('Kolkata').beginning_of_month.strftime("%Y-%m-%d") end (first..last).select { |d| valid_date?(d) }endp generate_date_range("2012-10-27", "2012-11-03")Output : ["2012-10-27", "2012-10-28", "2012-10-29", "2012-10-30", "2012-10-31", "2012-11-01", "2012-11-02", "2012-11-03"]Hope it helped you.Thank you. "> e false endend## Compute the first and last dates according to the params.## @param first [String] [First date]# @param last [String] [Last date]## @return [Array] [Array include first and last date]def generate_date_range(first, last) first, last = "", first unless last if last.nil? || last.empty? last = (Time.now - 1.day).in_time_zone('Kolkata').strftime("%Y-%m-%d") end if first.empty? first = Time.strptime(last, "%Y-%m-%d").in_time_zone('Kolkata').beginning_of_month.strftime("%Y-%m-%d") end (first..last).select { |d| valid_date?(d) }endp generate_date_range("2012-10-27", "2012-11-03")Output : ["2012-10-27", "2012-10-28", "2012-10-29", "2012-10-30", "2012-10-31", "2012-11-01", "2012-11-02", "2012-11-03"]Hope it helped you.Thank you. ">

Ruby : generate a date range

30 Nov 2012


    This is a simple post just to demonstrate how to get all the valid dates in between two given dates. Consider the situation that you have to loop through each date between 2012-10-27 and 2012-11-03. Please find below the code which I used to achieve this.


Output :

Hope it helped you.
Thank you.
If you particularly enjoy my work, I appreciate donations given with Gittip.