Get first beginning_of_week of a particular month - ruby-on-rails
If I want to know at what date last month had it's first monday, i.e. the month's first beginning_of_week, I could start by understanding what last was:
Date.current - 1.month
And as well get the beginning of that month:
(Date.current - (1.month)).beginning_of_month
But how could I efficiently get on what date the first week started in that month?
You can do the following:
date = (Date.current - (1.month)).beginning_of_month
date += 1.days until date.wday == 1 # wday 1 is monday
# why you english people consider Sunday as the first day of the week ?! ahah
The output in my IRB console:
1.9.3p489> date = (Date.current - (1.month)).beginning_of_month
# => Sun, 01 Dec 2013
1.9.3p489> date += 1.days until date.wday == 1
# => nil
1.9.3p489> date
# => Mon, 02 Dec 2013
Try
date = (Date.current - (1.month)).beginning_of_month
date = date.next_week unless date.monday?
start = Date.current.beginning_of_month
start.upto(start.next_week).find(&:monday?)
#=> Mon, 06 Jan 2014
A method you could use would be
date_in_last_month = Time.now - 1.month
beginning_of_month = date_in_last_month.beginning_of_month
beginning_of_week = beginning_of_month.beginning_of_week
if beginning_of_week < beginning_of_month
beginning_of_week + 7.days
else
beginning_of_week
end
A little long winded but with some code golf I think it might do what you want.
If you have the 'chronic' gem you can do the following (credit)
require 'chronic'
date_start = Chronic.parse("Monday of first week of February")
you can do it manually
date = (Date.current - (1.month)).beginning_of_month
if date.wday != 1
date = date.end_of_week
date += date.day
end
try this one:
def first_monday(months_before)
start_of_month = months_before.months.ago.beginning_of_month.to_date
return start_of_month if start_of_month.monday?
start_of_month += 8 - start_of_month.cwday
end
first_monday 5
=> Mon, 05 Aug 2013
Just joining the beauty contest:
# first Monday of current month
(Date.current.beginning_of_month - 1.day).next_week
# "at what date last month had its first monday"
((Date.current - 1.month).beginning_of_month - 1.day).next_week
# sanity check:
500.times do |i|
ary << ((Date.current - i.months).beginning_of_month - 1.day).next_week
end
=>
[Mon, 02 Jan 2023,
Mon, 05 Dec 2022,
Mon, 07 Nov 2022,
Mon, 03 Oct 2022,
Mon, 05 Sep 2022,
Mon, 01 Aug 2022,
Mon, 04 Jul 2022,
Mon, 06 Jun 2022,
Mon, 02 May 2022,
Mon, 04 Apr 2022,
Mon, 07 Mar 2022,
Mon, 07 Feb 2022,
Mon, 03 Jan 2022,
Mon, 06 Dec 2021,
Mon, 01 Nov 2021,
Mon, 04 Oct 2021,
Mon, 06 Sep 2021,
Mon, 02 Aug 2021,
Mon, 05 Jul 2021,
Mon, 07 Jun 2021,
Mon, 03 May 2021,
Mon, 05 Apr 2021,
Mon, 01 Mar 2021,
Mon, 01 Feb 2021,
Mon, 04 Jan 2021,
Mon, 07 Dec 2020,
Mon, 02 Nov 2020,
Mon, 05 Oct 2020,
Mon, 07 Sep 2020,
Mon, 03 Aug 2020,
Mon, 06 Jul 2020,
Mon, 01 Jun 2020,
Mon, 04 May 2020,
Mon, 06 Apr 2020,
Mon, 02 Mar 2020,
Mon, 03 Feb 2020,
Mon, 06 Jan 2020,
Mon, 02 Dec 2019,
Mon, 04 Nov 2019,
Mon, 07 Oct 2019,
Mon, 02 Sep 2019,
Mon, 05 Aug 2019,
Mon, 01 Jul 2019,
Mon, 03 Jun 2019,
Mon, 06 May 2019,
Mon, 01 Apr 2019,
Mon, 04 Mar 2019,
Mon, 04 Feb 2019,
Mon, 07 Jan 2019,
Mon, 03 Dec 2018,
Mon, 05 Nov 2018,
Mon, 01 Oct 2018,
Mon, 03 Sep 2018,
Mon, 06 Aug 2018,
Mon, 02 Jul 2018,
Mon, 04 Jun 2018,
Mon, 07 May 2018,
Mon, 02 Apr 2018,
Mon, 05 Mar 2018,
Mon, 05 Feb 2018,
Mon, 01 Jan 2018,
Mon, 04 Dec 2017,
Mon, 06 Nov 2017,
Mon, 02 Oct 2017,
Mon, 04 Sep 2017,
Mon, 07 Aug 2017,
Mon, 03 Jul 2017,
Mon, 05 Jun 2017,
Mon, 01 May 2017,
Mon, 03 Apr 2017,
Mon, 06 Mar 2017,
Mon, 06 Feb 2017,
Mon, 02 Jan 2017,
Mon, 05 Dec 2016,
Mon, 07 Nov 2016,
Mon, 03 Oct 2016,
Mon, 05 Sep 2016,
Mon, 01 Aug 2016,
Mon, 04 Jul 2016,
Mon, 06 Jun 2016,
Mon, 02 May 2016,
Mon, 04 Apr 2016,
Mon, 07 Mar 2016,
Mon, 01 Feb 2016,
Mon, 04 Jan 2016,
Mon, 07 Dec 2015,
Mon, 02 Nov 2015,
Mon, 05 Oct 2015,
Mon, 07 Sep 2015,
Mon, 03 Aug 2015,
Mon, 06 Jul 2015,
Mon, 01 Jun 2015,
Mon, 04 May 2015,
Mon, 06 Apr 2015,
Mon, 02 Mar 2015,
Mon, 02 Feb 2015,
Mon, 05 Jan 2015,
Mon, 01 Dec 2014,
Mon, 03 Nov 2014,
Mon, 06 Oct 2014,
Mon, 01 Sep 2014,
Mon, 04 Aug 2014,
Mon, 07 Jul 2014,
Mon, 02 Jun 2014,
Mon, 05 May 2014,
Mon, 07 Apr 2014,
Mon, 03 Mar 2014,
Mon, 03 Feb 2014,
Mon, 06 Jan 2014,
Mon, 02 Dec 2013,
Mon, 04 Nov 2013,
Mon, 07 Oct 2013,
Mon, 02 Sep 2013,
Mon, 05 Aug 2013,
Mon, 01 Jul 2013,
Mon, 03 Jun 2013,
Mon, 06 May 2013,
Mon, 01 Apr 2013,
Mon, 04 Mar 2013,
Mon, 04 Feb 2013,
Mon, 07 Jan 2013,
Mon, 03 Dec 2012,
Mon, 05 Nov 2012,
Mon, 01 Oct 2012,
Mon, 03 Sep 2012,
Mon, 06 Aug 2012,
Mon, 02 Jul 2012,
Mon, 04 Jun 2012,
Mon, 07 May 2012,
Mon, 02 Apr 2012,
Mon, 05 Mar 2012,
Mon, 06 Feb 2012,
Mon, 02 Jan 2012,
Mon, 05 Dec 2011,
Mon, 07 Nov 2011,
Mon, 03 Oct 2011,
Mon, 05 Sep 2011,
Mon, 01 Aug 2011,
Mon, 04 Jul 2011,
Mon, 06 Jun 2011,
Mon, 02 May 2011,
Mon, 04 Apr 2011,
Mon, 07 Mar 2011,
Mon, 07 Feb 2011,
Mon, 03 Jan 2011,
Mon, 06 Dec 2010,
Mon, 01 Nov 2010,
Mon, 04 Oct 2010,
Mon, 06 Sep 2010,
Mon, 02 Aug 2010,
Mon, 05 Jul 2010,
Mon, 07 Jun 2010,
Mon, 03 May 2010,
Mon, 05 Apr 2010,
Mon, 01 Mar 2010,
Mon, 01 Feb 2010,
Mon, 04 Jan 2010,
Mon, 07 Dec 2009,
Mon, 02 Nov 2009,
Mon, 05 Oct 2009,
Mon, 07 Sep 2009,
Mon, 03 Aug 2009,
Mon, 06 Jul 2009,
Mon, 01 Jun 2009,
Mon, 04 May 2009,
Mon, 06 Apr 2009,
Mon, 02 Mar 2009,
Mon, 02 Feb 2009,
Mon, 05 Jan 2009,
Mon, 01 Dec 2008,
Mon, 03 Nov 2008,
Mon, 06 Oct 2008,
Mon, 01 Sep 2008,
Mon, 04 Aug 2008,
Mon, 07 Jul 2008,
Mon, 02 Jun 2008,
Mon, 05 May 2008,
Mon, 07 Apr 2008,
Mon, 03 Mar 2008,
Mon, 04 Feb 2008,
Mon, 07 Jan 2008,
Mon, 03 Dec 2007,
Mon, 05 Nov 2007,
Mon, 01 Oct 2007,
Mon, 03 Sep 2007,
Mon, 06 Aug 2007,
Mon, 02 Jul 2007,
Mon, 04 Jun 2007,
Mon, 07 May 2007,
Mon, 02 Apr 2007,
Mon, 05 Mar 2007,
Mon, 05 Feb 2007,
Mon, 01 Jan 2007,
Mon, 04 Dec 2006,
Mon, 06 Nov 2006,
Mon, 02 Oct 2006,
Mon, 04 Sep 2006,
Mon, 07 Aug 2006,
Mon, 03 Jul 2006,
Mon, 05 Jun 2006,
Mon, 01 May 2006,
Mon, 03 Apr 2006,
Mon, 06 Mar 2006,
Mon, 06 Feb 2006,
Mon, 02 Jan 2006,
Mon, 05 Dec 2005,
Mon, 07 Nov 2005,
Mon, 03 Oct 2005,
Mon, 05 Sep 2005,
Mon, 01 Aug 2005,
Mon, 04 Jul 2005,
Mon, 06 Jun 2005,
Mon, 02 May 2005,
Mon, 04 Apr 2005,
Mon, 07 Mar 2005,
Mon, 07 Feb 2005,
Mon, 03 Jan 2005,
Mon, 06 Dec 2004,
Mon, 01 Nov 2004,
Mon, 04 Oct 2004,
Mon, 06 Sep 2004,
Mon, 02 Aug 2004,
Mon, 05 Jul 2004,
Mon, 07 Jun 2004,
Mon, 03 May 2004,
Mon, 05 Apr 2004,
Mon, 01 Mar 2004,
Mon, 02 Feb 2004,
Mon, 05 Jan 2004,
Mon, 01 Dec 2003,
Mon, 03 Nov 2003,
Mon, 06 Oct 2003,
Mon, 01 Sep 2003,
Mon, 04 Aug 2003,
Mon, 07 Jul 2003,
Mon, 02 Jun 2003,
Mon, 05 May 2003,
Mon, 07 Apr 2003,
Mon, 03 Mar 2003,
Mon, 03 Feb 2003,
Mon, 06 Jan 2003,
Mon, 02 Dec 2002,
Mon, 04 Nov 2002,
Mon, 07 Oct 2002,
Mon, 02 Sep 2002,
Mon, 05 Aug 2002,
Mon, 01 Jul 2002,
Mon, 03 Jun 2002,
Mon, 06 May 2002,
Mon, 01 Apr 2002,
Mon, 04 Mar 2002,
Mon, 04 Feb 2002,
Mon, 07 Jan 2002,
Mon, 03 Dec 2001,
Mon, 05 Nov 2001,
Mon, 01 Oct 2001,
Mon, 03 Sep 2001,
Mon, 06 Aug 2001,
Mon, 02 Jul 2001,
Mon, 04 Jun 2001,
Mon, 07 May 2001,
Mon, 02 Apr 2001,
Mon, 05 Mar 2001,
Mon, 05 Feb 2001,
Mon, 01 Jan 2001,
Mon, 04 Dec 2000,
Mon, 06 Nov 2000,
Mon, 02 Oct 2000,
Mon, 04 Sep 2000,
Mon, 07 Aug 2000,
Mon, 03 Jul 2000,
Mon, 05 Jun 2000,
Mon, 01 May 2000,
Mon, 03 Apr 2000,
Mon, 06 Mar 2000,
Mon, 07 Feb 2000,
Mon, 03 Jan 2000,
Mon, 06 Dec 1999,
Mon, 01 Nov 1999,
Mon, 04 Oct 1999,
Mon, 06 Sep 1999,
Mon, 02 Aug 1999,
Mon, 05 Jul 1999,
Mon, 07 Jun 1999,
Mon, 03 May 1999,
Mon, 05 Apr 1999,
Mon, 01 Mar 1999,
Mon, 01 Feb 1999,
Mon, 04 Jan 1999,
Mon, 07 Dec 1998,
Mon, 02 Nov 1998,
Mon, 05 Oct 1998,
Mon, 07 Sep 1998,
Mon, 03 Aug 1998,
Mon, 06 Jul 1998,
Mon, 01 Jun 1998,
Mon, 04 May 1998,
Mon, 06 Apr 1998,
Mon, 02 Mar 1998,
Mon, 02 Feb 1998,
Mon, 05 Jan 1998,
Mon, 01 Dec 1997,
Mon, 03 Nov 1997,
Mon, 06 Oct 1997,
Mon, 01 Sep 1997,
Mon, 04 Aug 1997,
Mon, 07 Jul 1997,
Mon, 02 Jun 1997,
Mon, 05 May 1997,
Mon, 07 Apr 1997,
Mon, 03 Mar 1997,
Mon, 03 Feb 1997,
Mon, 06 Jan 1997,
Mon, 02 Dec 1996,
Mon, 04 Nov 1996,
Mon, 07 Oct 1996,
Mon, 02 Sep 1996,
Mon, 05 Aug 1996,
Mon, 01 Jul 1996,
Mon, 03 Jun 1996,
Mon, 06 May 1996,
Mon, 01 Apr 1996,
Mon, 04 Mar 1996,
Mon, 05 Feb 1996,
Mon, 01 Jan 1996,
Mon, 04 Dec 1995,
Mon, 06 Nov 1995,
Mon, 02 Oct 1995,
Mon, 04 Sep 1995,
Mon, 07 Aug 1995,
Mon, 03 Jul 1995,
Mon, 05 Jun 1995,
Mon, 01 May 1995,
Mon, 03 Apr 1995,
Mon, 06 Mar 1995,
Mon, 06 Feb 1995,
Mon, 02 Jan 1995,
Mon, 05 Dec 1994,
Mon, 07 Nov 1994,
Mon, 03 Oct 1994,
Mon, 05 Sep 1994,
Mon, 01 Aug 1994,
Mon, 04 Jul 1994,
Mon, 06 Jun 1994,
Mon, 02 May 1994,
Mon, 04 Apr 1994,
Mon, 07 Mar 1994,
Mon, 07 Feb 1994,
Mon, 03 Jan 1994,
Mon, 06 Dec 1993,
Mon, 01 Nov 1993,
Mon, 04 Oct 1993,
Mon, 06 Sep 1993,
Mon, 02 Aug 1993,
Mon, 05 Jul 1993,
Mon, 07 Jun 1993,
Mon, 03 May 1993,
Mon, 05 Apr 1993,
Mon, 01 Mar 1993,
Mon, 01 Feb 1993,
Mon, 04 Jan 1993,
Mon, 07 Dec 1992,
Mon, 02 Nov 1992,
Mon, 05 Oct 1992,
Mon, 07 Sep 1992,
Mon, 03 Aug 1992,
Mon, 06 Jul 1992,
Mon, 01 Jun 1992,
Mon, 04 May 1992,
Mon, 06 Apr 1992,
Mon, 02 Mar 1992,
Mon, 03 Feb 1992,
Mon, 06 Jan 1992,
Mon, 02 Dec 1991,
Mon, 04 Nov 1991,
Mon, 07 Oct 1991,
Mon, 02 Sep 1991,
Mon, 05 Aug 1991,
Mon, 01 Jul 1991,
Mon, 03 Jun 1991,
Mon, 06 May 1991,
Mon, 01 Apr 1991,
Mon, 04 Mar 1991,
Mon, 04 Feb 1991,
Mon, 07 Jan 1991,
Mon, 03 Dec 1990,
Mon, 05 Nov 1990,
Mon, 01 Oct 1990,
Mon, 03 Sep 1990,
Mon, 06 Aug 1990,
Mon, 02 Jul 1990,
Mon, 04 Jun 1990,
Mon, 07 May 1990,
Mon, 02 Apr 1990,
Mon, 05 Mar 1990,
Mon, 05 Feb 1990,
Mon, 01 Jan 1990,
Mon, 04 Dec 1989,
Mon, 06 Nov 1989,
Mon, 02 Oct 1989,
Mon, 04 Sep 1989,
Mon, 07 Aug 1989,
Mon, 03 Jul 1989,
Mon, 05 Jun 1989,
Mon, 01 May 1989,
Mon, 03 Apr 1989,
Mon, 06 Mar 1989,
Mon, 06 Feb 1989,
Mon, 02 Jan 1989,
Mon, 05 Dec 1988,
Mon, 07 Nov 1988,
Mon, 03 Oct 1988,
Mon, 05 Sep 1988,
Mon, 01 Aug 1988,
Mon, 04 Jul 1988,
Mon, 06 Jun 1988,
Mon, 02 May 1988,
Mon, 04 Apr 1988,
Mon, 07 Mar 1988,
Mon, 01 Feb 1988,
Mon, 04 Jan 1988,
Mon, 07 Dec 1987,
Mon, 02 Nov 1987,
Mon, 05 Oct 1987,
Mon, 07 Sep 1987,
Mon, 03 Aug 1987,
Mon, 06 Jul 1987,
Mon, 01 Jun 1987,
Mon, 04 May 1987,
Mon, 06 Apr 1987,
Mon, 02 Mar 1987,
Mon, 02 Feb 1987,
Mon, 05 Jan 1987,
Mon, 01 Dec 1986,
Mon, 03 Nov 1986,
Mon, 06 Oct 1986,
Mon, 01 Sep 1986,
Mon, 04 Aug 1986,
Mon, 07 Jul 1986,
Mon, 02 Jun 1986,
Mon, 05 May 1986,
Mon, 07 Apr 1986,
Mon, 03 Mar 1986,
Mon, 03 Feb 1986,
Mon, 06 Jan 1986,
Mon, 02 Dec 1985,
Mon, 04 Nov 1985,
Mon, 07 Oct 1985,
Mon, 02 Sep 1985,
Mon, 05 Aug 1985,
Mon, 01 Jul 1985,
Mon, 03 Jun 1985,
Mon, 06 May 1985,
Mon, 01 Apr 1985,
Mon, 04 Mar 1985,
Mon, 04 Feb 1985,
Mon, 07 Jan 1985,
Mon, 03 Dec 1984,
Mon, 05 Nov 1984,
Mon, 01 Oct 1984,
Mon, 03 Sep 1984,
Mon, 06 Aug 1984,
Mon, 02 Jul 1984,
Mon, 04 Jun 1984,
Mon, 07 May 1984,
Mon, 02 Apr 1984,
Mon, 05 Mar 1984,
Mon, 06 Feb 1984,
Mon, 02 Jan 1984,
Mon, 05 Dec 1983,
Mon, 07 Nov 1983,
Mon, 03 Oct 1983,
Mon, 05 Sep 1983,
Mon, 01 Aug 1983,
Mon, 04 Jul 1983,
Mon, 06 Jun 1983,
Mon, 02 May 1983,
Mon, 04 Apr 1983,
Mon, 07 Mar 1983,
Mon, 07 Feb 1983,
Mon, 03 Jan 1983,
Mon, 06 Dec 1982,
Mon, 01 Nov 1982,
Mon, 04 Oct 1982,
Mon, 06 Sep 1982,
Mon, 02 Aug 1982,
Mon, 05 Jul 1982,
Mon, 07 Jun 1982,
Mon, 03 May 1982,
Mon, 05 Apr 1982,
Mon, 01 Mar 1982,
Mon, 01 Feb 1982,
Mon, 04 Jan 1982,
Mon, 07 Dec 1981,
Mon, 02 Nov 1981,
Mon, 05 Oct 1981,
Mon, 07 Sep 1981,
Mon, 03 Aug 1981,
Mon, 06 Jul 1981,
Mon, 01 Jun 1981]
Related
How to create dates of a particular month
I want to generate all the dates of a particular month starting from the 1st of that month to the 30 or 31st of that month with out using a trivial for loop: for i in 1..Time.days_in_month(Date.today.month, Date.today.year) date = Date.new(y, m, i) end I was getting dates like this but I'm not supposed to use this trivial for loop.
Although there are quite a few ways you could accomplish this, one way would be to use a Ruby Range combined with the Time#beginning_of_month and Time#end_of_month methods to generate an object that contains all of the days of the month and then convert it into an array: (Date.today.beginning_of_month..Date.today.end_of_month).to_a => [Fri, 01 Nov 2019, Sat, 02 Nov 2019, Sun, 03 Nov 2019, Mon, 04 Nov 2019, Tue, 05 Nov 2019, Wed, 06 Nov 2019, ...., Fri, 29 Nov 2019, Sat, 30 Nov 2019] Another option would be to use the Time#all_month helper which is as simple as: Date.today.all_month.to_a => [Fri, 01 Nov 2019, Sat, 02 Nov 2019, Sun, 03 Nov 2019, Mon, 04 Nov 2019, Tue, 05 Nov 2019, Wed, 06 Nov 2019, ...., Fri, 29 Nov 2019, Sat, 30 Nov 2019]
(Plain Ruby) The Date class uses negative numbers as a way of counting from the end (like in Arrays): require "date" today = Date.today m, y = today.month, today.year p (Date.new(y,m,1) .. Date.new(y,m,-1)).to_a
Subtracting two time ranges
I have two timeranges Fri, 02 Aug 2019 10:09:58 UTC +00:00..Fri, 02 Aug 2019 23:59:59 UTC +00:00 Fri, 02 Aug 2019 11:09:58 UTC +00:00..Fri, 02 Aug 2019 12:09:58 UTC +00:00 What's the simplest way to subtract the second from the first so I get [ Fri, 02 Aug 2019 10:09:58 UTC +00:00..Fri, 02 Aug 2019 11:09:58 UTC +00:00, Fri, 02 Aug 2019 12:09:58 UTC +00:00..Fri, 02 Aug 2019 23:59:59 UTC +00:00 ]
I would do something like this: range_1 = (Time.parse('2019-08-02 10:09:58 UTC') .. Time.parse('2019-08-02 23:59:59 UTC')) range_2 = (Time.parse('2019-08-02 11:09:58 UTC') .. Time.parse('2019-08-02 12:09:58 UTC')) [(range_1.begin..range_2.begin), (range_2.end..range_1.end)] #=> [2019-08-02 10:09:58 UTC..2019-08-02 11:09:58 UTC, 2019-08-02 12:09:58 UTC..2019-08-02 23:59:59 UTC]
Generate random datetimes in rails with the minutes belongs to range(00, 30)
Event model which has start and end datetime attributes in the database. I want to seed some random events but the event time should be proper. For example: 6.times { date_range << DateTime.now + (rand * 21) } generates [Thu, 03 Aug 2017 21:22:48 +0530, Tue, 08 Aug 2017 17:36:29 +0530, Sat, 29 Jul 2017 06:19:51 +0530, Sat, 29 Jul 2017 13:36:21 +0530, Thu, 27 Jul 2017 15:08:55 +0530, Fri, 04 Aug 2017 13:53:03 +0530] which is the correct behaviour. But how to generate random datetime like this: [Thu, 03 Aug 2017 21:00:00 +0530, Tue, 08 Aug 2017 17:30:00 +0530, Sat, 29 Jul 2017 06:00:00 +0530, Sat, 29 Jul 2017 13:00:00 +0530, Thu, 27 Jul 2017 15:30:00 +0530, Fri, 04 Aug 2017 13:00:00 +0530] So in order to display these events properly on a calendar.
Could try separating out each segment and adding them onto the date individually date_range = 6.times.collect do DateTime.now.beginning_of_day + # starting from today rand(21).days + # pick a random day, no further than 3 weeks out rand(24).hours + # move forward to a random hour on that day (rand(2) * 30).minutes # and then decide whether to add 30 minutes end or, could combine the hours + minutes date_range = 6.times.collect do DateTime.now.beginning_of_day + # starting from today rand(21).days + # pick a random day, no further than 3 weeks out (rand(48) * 30).minutes # pick a random interval of 30 minutes to add in end
Found the working solution but not complete: 6.times { date_range << DateTime.parse((DateTime.now + (rand * 21)).beginning_of_hour.to_s) } [Mon, 31 Jul 2017 06:00:00 +0530, Thu, 03 Aug 2017 15:00:00 +0530, Fri, 11 Aug 2017 14:00:00 +0530, Mon, 31 Jul 2017 09:00:00 +0530, Wed, 09 Aug 2017 16:00:00 +0530, Sat, 12 Aug 2017 13:00:00 +0530] This can work for now but need some datetime with 30 minutes as well.
How to get all the DATES between two input DATES In Rails
The user inputs the starting date and the end date. I want to get the dates between these two input dates. I tried this: (datestart..dateend).to_a but it returns the whole month, and when I choose from the previous year, it gives an error ArgumentError (invalid date):. This is the example return when I choose Jan. 1 2017 and Jan. 2 2017 - the result was whole month and an inaccurate dates. [Tue, 12 Jan 2016, Wed, 13 Jan 2016, Thu, 14 Jan 2016, Fri, 15 Jan 2016, Sat, 16 Jan 2016, Sun, 17 Jan 2016, Mon, 18 Jan 2016, Tue, 19 Jan 2016, Wed, 20 Jan 2016, Thu, 21 Jan 2016, Fri, 22 Jan 2016, Sat, 23 Jan 2016, Sun, 24 Jan 2016, Mon, 25 Jan 2016, Tue, 26 Jan 2016, Wed, 27 Jan 2016, Thu, 28 Jan 2016, Fri, 29 Jan 2016, Sat, 30 Jan 2016, Sun, 31 Jan 2016, Mon, 01 Feb 2016, Tue, 02 Feb 2016, Wed, 03 Feb 2016, Thu, 04 Feb 2016, Fri, 05 Feb 2016, Sat, 06 Feb 2016, Sun, 07 Feb 2016, Mon, 08 Feb 2016, Tue, 09 Feb 2016, Wed, 10 Feb 2016, Thu, 11 Feb 2016, Fri, 12 Feb 2016] Update! I Fix it by using Date.strptime(params[:datestart_stat], '%m/%d/%Y')
start_date = Date.parse('date start') end_date = Date.parse('date end') (start..endd).to_a
Update! I was able to fix my mistake now. I just change the Date.parse(params[:datestart_stat]) # to Date.strptime(params[:datestart_stat], '%m/%d/%Y') thank you guys.
Rails ActiveRecord group by time but pad dates with nil values?
So I'm using activerecord to get the count of items for a certain day: Item.group("date(created_at)").count.values But this doesn't give me 0 if there are no records for a given day, how can I fix this? Or is there are more "Rails" way do to something like this? Any help is hugely appreciated, thanks again!
Using a GROUP BY query, you are going to get groups based on the records that exist, so if there are no records with a given value in the grouping column, there will be no groups for that value. The database does not psychically know to add in other date values that you might be interested in. What I would do to get what you want is create a new Hash with a default value of 0 and then merge the result of your query into that. When you subsequently try to retrieve any value from that Hash for which there is no entry, you'll get 0 as the result. Edit: Remove .values date_counts = Hash.new(0) date_counts.merge! Item.group("date(created_at)").count Alternatively, if you want to make sure there are entries in the Hash for every date in a range, so you can iterate over those, ... date_counts = {} (Date.civil(2010,1,1)...Date.civil(2010,2,1)).each do |date| date_counts[date]=0 end date_counts.merge! Item.group("date(created_at)").count Update Here is some output when I pass my date range and model into here, oddly enough it gets different length hashes: {Sun, 01 May 2011=>0, Mon, 02 May 2011=>0, Tue, 03 May 2011=>0, Wed, 04 May 2011=>0, Thu, 05 May 2011=>0, Fri, 06 May 2011=>0, Sat, 07 May 2011=>0, Sun, 08 May 2011=>0, Mon, 09 May 2011=>0, Tue, 10 May 2011=>0, Wed, 11 May 2011=>0, Thu, 31 Mar 2011=>2, Sun, 03 Apr 2011=>1, Fri, 08 Apr 2011=>643, Sat, 09 Apr 2011=>2360, Sun, 10 Apr 2011=>705, Mon, 11 Apr 2011=>34} {Sun, 01 May 2011=>0, Mon, 02 May 2011=>0, Tue, 03 May 2011=>0, Wed, 04 May 2011=>0, Thu, 05 May 2011=>0, Fri, 06 May 2011=>0, Sat, 07 May 2011=>0, Sun, 08 May 2011=>0, Mon, 09 May 2011=>0, Tue, 10 May 2011=>0, Wed, 11 May 2011=>0, Sat, 02 Apr 2011=>1, Fri, 08 Apr 2011=>4158, Sat, 09 Apr 2011=>12206, Sun, 10 Apr 2011=>4279, Mon, 11 Apr 2011=>169} {Sun, 01 May 2011=>0, Mon, 02 May 2011=>0, Tue, 03 May 2011=>0, Wed, 04 May 2011=>0, Thu, 05 May 2011=>0, Fri, 06 May 2011=>0, Sat, 07 May 2011=>0, Sun, 08 May 2011=>0, Mon, 09 May 2011=>0, Tue, 10 May 2011=>0, Wed, 11 May 2011=>0} Not sure whats causing it... hmm.