Rails, day of year to date - ruby-on-rails

I have day of the year. For example 15th day of 2014, or 210th day of 2014. I need to get the Date on the particular day. Is there any library function in Rails/Ruby that I can use, or any other elegant way?
Something like:
15th day of 2014 = 15-Jan-2014
210th day of 2014 = 29-Jul-2014

You can use Date.ordinal for that:
require 'date'
Date.ordinal(2014, 210)
# => #<Date: 2014-07-29 ((2456868j,0s,0n),+0s,2299161j)>

Date.new(2014, 1, 1) + 210
=> Wed, 30 Jul 2014
Edit - you'd need to subtract 1 day. I prefer #wonderb0lt's suggestion.

Related

How to parse week and year with strptime

Usually I don't have issues parsing strings to Time in Rails, however I cannot understand why it's behaving like this:
irb(main):073:0> DateTime.strptime("20/2020", "%V/%Y")
=> Wed, 01 Jan 2020 00:00:00 +0000
I want the 20th week of 2020, not the 1st Jan...
I think that you need to use the commercial method.
require 'date'
week = 20
start_week = Date.commercial(2020, week)
# => Mon, 11 May 2020
puts "#{start_week}"
# => 2020-05-11
In case you need a Range, you can do:
Date.commercial(2020, week).all_week
# => Mon, 11 May 2020..Sun, 17 May 2020
For more information see the documentation.
require 'date'
If week 1 begins on Sunday:
Date.strptime("20/2020", "%U/%Y")
#=> #<Date: 2020-05-17 ((2458987j,0s,0n),+0s,2299161j)>
and on Monday:
d = Date.strptime("20/2020", "%W/%Y")
#=> #<Date: 2020-05-18 ((2458988j,0s,0n),+0s,2299161j)>
and also from the doc Date#strftime (which contains the formatting directives for Date::strptime), "The days in the year before the first week are in week 0".
There is generally a difference between %Y and %G for the year depending on your assumed definition for a week number.
For ISO 8601 weeks (which are commonly used in e.g. Europe), you have to use the week-based year here (%G) rather than the day-=based year (%Y). This is important around the edges of the years. For example, 2021-01-01 is in week 53 of 2020. On this date
%G is 2020, but
%Y is already 2021
If you want to parse an ISO 8601 week, you can thus use the following code to ensur you use the right week and year:
require 'date'
DateTime.strptime("20/2020", "%V/%G")
# => #<DateTime: 2020-05-11T00:00:00+00:00 ((2458981j,0s,0n),+0s,2299161j)>

Query all records from a week in a month

I am creating a Rails 5 app.
In this app I got Survey model. I am able to run queries (using scopes) to get all surveys from a specific month and all from a specific quarter but I want to get all from a specific week in a month too, how can I do that?
These are my quarter and month scopes
scope :period_quarter, -> (year, quarter) { where(created_at: Date.new(year.to_i, 3 * quarter.to_i - 2).all_quarter) }
scope :period_month, -> (year, month) { where(created_at: Date.new(year.to_i, month.to_i).all_month) }
How can I add a scope to get all surveys from a specific week in a month. I will provide year, month and week (1-5).
Consider the same approach you've been using, only with all_week and perhaps beginning_of_week as needed to get the correct start date.
To get a date in a specific week, you could use something like this:
def week(year, month, week_num)
Date.new(year, month, 7 * week_num - 6)
end
week(2018, 8, 1).beginning_of_week # => Mon, 30 Jul 2018
week(2018, 8, 1).end_of_week # => Sun, 05 Aug 2018
week(2018, 8, 1).all_week # => Mon, 30 Jul 2018..Sun, 05 Aug 2018
To use this in a query, you could use it like this:
where(created_at: week(2018, 8, 1).all_week)
This will count the first week in a month as the week where the 1. is. However, if you want it to use the first full week in a month, you can remove the -6.

How to get time x days ago from a specific date in Rails?

I am looking for a rails solution to calculate the time ago from a particulat time. For example , 2 days ago 15th May 2016 22:00 UTC should return 13th May 2016 22::00 UTC .
My requirement is something like this
2.days.ago.from(yesterday)
Which will be a more specific version of
2.days.from_now
Try this:
> DateTime.now-2.days
=> Wed, 18 May 2016 21:40:31 -0700
how about this:
# 2 days before a specific date
specific_date.days_ago(2)
Example:
specific_date = DateTime.now
two_days_ago_from_specific_date = specific_date.days_ago(2)
My personal favorite syntax for this with rails would be
x.days.ago
For example, if you wanted 10 days ago you would call
10.days.ago
=> Sat, 12 Feb 2022 01:36:58 UTC +00:00
Easy to read and defaults to UTC.
example:
Time.now.ago(10.year)
Time.now.ago(1.minutes)
Time.now.ago(10.day)

Traverse through a certain month in Date with just a given integer

Would it be possible to go to a certain month of the year with just a given integer. For example
date = Date.today
=> Wed, 30 Dec 2015
What if I want to go back to a certain month based on that date and I am just given a number let's say 7 which is July in the Date::MONTHNAMES so would it be possible to do something like
date = Date.today
=> Wed, 30 Dec 2015
date.go_to_month_of(7) # which will bring me back to July 30, 2015
Okay I found it. It's:
date = Date.today
date.change(:month => x)
Hope this helps you!

get one month ago but always 15th of month

I need to get the month before today, but always to get the date with 15th.
i.e if today is March 11th 2014, I need => Feb 15th 2014.
or if the date is March 28th 2014, I need => Feb 15th 2014.
How can I achieve that?
Since you're using rails:
(DateTime.now - 1.month).beginning_of_month + 14.days
Refer to the DateTime documentation for more info.
Edit: Updated to reflect the previous month's 15th day.
(Date.today - 1.month).beginning_of_month + 14
or another way
(Date.today - 1.month).change(day: 15)
You can just create a new date object with last month's year and month numbers and 15 as the day:
today = Date.today
one_month_ago = (today - 1.month)
Date.civil(one_month_ago.year, one_month_ago.month, 15)
Do it like this:
Date.today.prev_month.beginning_of_month + 14 # => Sat, 15 Feb 2014
Use
(Date.today<<1).beginning_of_month + 14 ## => Sat, 15 Feb 2014
Or
Date.today.last_month.beginning_of_month + 14 ## => Sat, 15 Feb 2014

Resources