Try this with latest Rails (6.0.2.1):
Date.new(2020,2,29) + 1.year + 1.year + 1.year + 1.year
=> Wed, 28 Feb 2024
Date.new(2020,2,29) + 4.years
=> Thu, 29 Feb 2024
or this one:
Date.new(2020,2,28) + 1.year
=> Sun, 28 Feb 2021
Date.new(2020,2,29) + 1.year
=> Sun, 28 Feb 2021
Bug or feature?
From my understanding it is not a bug at all.
In first case ruby adds each year to previous. As we know Date.new(2021,2,29) is invalid date, the valid date is Date.new(2021,2,28) so result is totally expected. Adding 4 years as one operation seems fair and logic also, result is Thu, 29 Feb 2024, but if you add only 3 years the result will be Tue, 28 Feb 2023.
Related
I need to get the Current Sunday date from todays date.
I have tried this but it gives previous week Sunday, expected date is 11 Oct 2020 (today is 13 Oct 2020)
Here are Today Vs Expected Values
Formula that uses this Date -> Result
10 Oct -> 04 Oct
11 Oct -> 11 Oct
12 Oct -> 11 Oct
13 Oct -> 11 Oct
14 Oct -> 11 Oct
15 Oct -> 11 Oct
16 Oct -> 11 Oct
17 Oct -> 11 Oct
18 Oct -> 18 Oct
19 Oct -> 18 Oct
=TODAY() - (WEEKDAY(TODAY()) - 1) - 7
With dates in column A, in B1 enter:
=A1-(WEEKDAY(A1)-1)
I have tried this but it gives previous week Sunday, expected date is
11 Oct 2020 (today is 13 Oct 2020)
You don't need -7. Simply try:
=TODAY() - (WEEKDAY(TODAY()) - 1)
and as the other answer suggests, replace Today() with the desired dates.
I have a range with a start_date, end_date and I want to get the same day of each month for the whole range, so here starting on the 30th of January I should get the 30th of each month:
start_date = Date.new(2019, 1, 30)
end_date = Date.new(2019, 12, 30)
range = (start_date...end_date)
dates = range.step(30).map(&:to_date)
dates
#=> [Wed, 30 Jan 2019,
# Fri, 01 Mar 2019,
# Sun, 31 Mar 2019,
# Tue, 30 Apr 2019,
# Thu, 30 May 2019,
# Sat, 29 Jun 2019,
# Mon, 29 Jul 2019,
# Wed, 28 Aug 2019,
# Fri, 27 Sep 2019,
# Sun, 27 Oct 2019,
# Tue, 26 Nov 2019,
# Thu, 26 Dec 2019]
I was using something like this for weeks but with months when you get to February for example it of course fails, so I would have to adjust to 28th.
I know I could loop and look at the month and do adjustments based on the start_date but it feels like a bad idea.
I think you can use either active support:
require 'active_support/time'
start_date = Date.parse('2019-10-31')
12.times.map { |i| start_date + i.month }
=> [
Thu, 31 Oct 2019,
Sat, 30 Nov 2019,
Tue, 31 Dec 2019,
Fri, 31 Jan 2020,
Sat, 29 Feb 2020,
Tue, 31 Mar 2020,
Thu, 30 Apr 2020,
Sun, 31 May 2020,
Tue, 30 Jun 2020,
Fri, 31 Jul 2020,
Mon, 31 Aug 2020,
Wed, 30 Sep 2020
]
or adjust: #next_month:
require 'date'
Date.parse('2019-10-31').next_month # => Sat, 30 Nov 2019
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.
I have date as "Wed, 29 Jun 2016" and time in "11:35 PM" format
how can i create a date time object with it?
something like what Time.current does.
It's usually pretty simple if your date can be parsed:
DateTime.parse("Wed, 29 Jun 2016 11:35 PM")
# => Wed, 29 Jun 2016 23:35:00 +0000
You can then use that in any capacity you'd use any other date/time.
Why you don't use next:
Time.parse('Wed, 29 Jun 2016 11:35 PM')
=> 2016-06-29 23:35:00 +0300
I got this error and makes me crazy.
My desired output is shows the week starting at sunday and after that calculate 10 weeks ago based on that.
Example:
[Sun, 12 Aug 2012, Sun, 05 Aug 2012, Sun, 29 Jul 2012, Sun, 22 Jul
2012, Sun, 15 Jul 2012, Sun, 08 Jul 2012, Sun, 01 Jul 2012, Sun, 24
Jun 2012, Sun, 17 Jun 2012, Sun, 10 Jun 2012, Sun, 03 Jun 2012]
But at my machine it's correctly and return the array above, however at the server is wrong :(
The output at server is:
[Sat, 11 Aug 2012, Sat, 04 Aug 2012, Sat, 28 Jul 2012, Sat, 21 Jul
2012, Sat, 14 Jul 2012, Sat, 07 Jul 2012, Sat, 30 Jun 2012, Sat, 23
Jun 2012, Sat, 16 Jun 2012, Sat, 09 Jun 2012, Sat, 02 Jun 2012]
If I access the app and call the controller from script/console shows wrong but If I recalculate at script/console shows correctly.
My environment:
OS X 10.7.4, ruby 1.8.7 (2012-04-14 patchlevel 361)
[i686-darwin11.3.0], rvm 1.10.2, Seg 13 Ago 2012 09:27:46 BRT (system
date)
And server environment:
Ubuntu 12.04, ruby 1.8.7 (2012-02-08 MBARI 8/0x8770 on patchlevel 358)
[i686-linux], MBARI 0x8770, Ruby Enterprise Edition 2012.02, rvm
1.14.0 (stable), Mon Aug 13 13:29:13 WEST 2012
Probably a time zone mismatch on your server, try setting your time zone explicitly in config/appliction.rb:
config.time_zone = "Pacific Time (US & Canada)"
A popular solution to this issue is setting a before_filter on your controller that configures the right time zone per user. See: http://railscasts.com/episodes/106-time-zones-in-rails-2-1 for a starting point.