Calculate correct start date for the below scenarios? - ruby-on-rails

I have below condition.
Order.where('created_at BETWEEN ? AND ?', start_date, end_date)
I want orders between start_date and end_date
But I want to write query for below scenarios.
If today's date is greater than 3rd July 2021 then I want the orders between 1st July 2021 to today's date.
If today's date is less than 3rd July 2021 then I want the orders from last month(1st June 2021) to 3rd July 2021.
How to calculate correct start date?

start_date = (Date.today > Date.new(Date.today.year, Date.today.month,3)) ? Date.today.at_beginning_of_month : Date.today.prev_month.at_beginning_of_month

Related

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 data which falls on specific days

Hi I've start_date column which capture time stamp, I'd like to query the data based on days like if date falls on sunday or monday like that.
For example
from_date = Tue Nov 22 23:00:00 UTC 2016
to_date = Sun Nov 27 02:00:00 UTC 2016
Here I need get all the records from tuesday 23:00 pm to sunday 02:00 am.
Updated Answer
MySQL has dayofweek function for this.
You can write your query as:
MyModel.where('dayofweek(start_date) = ?', 1) # 1 is Sunday
To get all models which start_date is on Sunday.
To achieve your goal, you can query start_date on Tuesday AND start_date hour is later than 23:00, start_date on Wed, start_date on Thurs and etc.
So the final query would be like:
MyModel.where('(dayofweek(start_date) = ? AND hour(start_date) > ?)
OR dayofweek(start_date) = ? OR dayofweek(start_date) = ? OR
dayofweek(start_date) = ? OR dayofweek(start_date) = ? OR
(dayofweek(start_date) = ? AND hour(start_date) > ?)'
, 3, 23, 4, 5, 6, 7, 1, 2)
Previous Answer
You can use a range query with where:
Model.where(start_date: from_date..to_date)

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!

Rank in-order of relation to the current date. [Rails, Sqlite3]

How do I return information in rank of closeness to the current date irrelevant of the year. If Current date is May 26 2011, the date closeness to May 26 is returned first. My problem was this confusion: I have two dates: Date 1: May 24th 2011 and Date 2: June 1st 2010, ensuring that Date 2 is returned before Date 1. Both because May 24 has passed and also the year factor.
Table:
Name Created_at
Bob 2010-06-01 (YYYY-MM-DD)
Mike 2010-05-07
Fife 2011-05-09
So it would return if current date is May 26 2011:
Bob 2010-06-01 (YYYY-MM-DD)
Mike 2010-05-07
Fife 2011-05-09
What I have so far:
#awesomeDate = Names.order("name").having("date > ?", Date.today)

How do I get a Date from a week number?

I want to find all items created in a given week, and pass in a week number param. (created_at is a normal timestamp.)
Given a week number, what is the easiest way to find a date in that particular week?
(Any date in the week will do, as I will use beginning_of_week and end_of_week in the scope.)
You can get Date objects representing the beginning and end of your week using the commercial method:
week = 41;
wkBegin = Date.commercial(2010, week, 1)
wkEnd = Date.commercial(2010, week, 7)
Now do your find:
Item.find(:all, :conditions->:create_date=>wkBegin..wkEnd.end_of_day)
Assuming you mean "a given week number in the current year", you can do the following:
2.weeks.since(Time.gm(Time.now.year))
=> Fri Jan 15 00:00:00 UTC 2010
Substitute (week_number - 1) for the 1 in the above, and you'll get a date in the desired week.

Resources