This question already has an answer here:
Compare Time.now in Rails but ignore year?
(1 answer)
Closed 4 years ago.
I have a birthday problem. I'm trying to find all users who have a birthday 5 days from now.
I can do something like the below but the year is causing problems..
#dobs = Time.now + 5.days
#users = User.where(dob: #dobs).all
I also tried to make the dob.year the current year but this breaks for birthdays in early Jan because 5 days earlier is the year before.
Is there a gem I can use, or is it something more obvious?
Try something like this:
User.where(dob: (Time.now + 5.days).all_day)
User.where(dob: (Date.today + 5.days).all_day) # or as Qwertie suggested
all_day will capture anything from the start to the end of the day.
Related
I have a bit of test that needs to only appear on my site during the months of Aug through Nov, and all other months the text should be something different. What is the best way to accomplish this?
I would think it would look something like the following, but I am not sure how to limit it to just the month, so that the switch will happen every year.
IF Date.today IS BETWEEN ???August AND ???Nov DO
...
ELSE
...
Use .month and check if it's value included into expected list of accepted months
accepted_months = Set.new([8, 9])
if accepted_months.include?(Time.current.month)
else
end
This question already has answers here:
Rails formatting date
(4 answers)
Closed 4 years ago.
I have a variable containing a date in this form: (2018-03-21 18:49:49 UTC)
and I would like to display in this form: (day / month / year) but I can not find a solution. Do you have an idea ? Thank you !
Well, I guess this could work.
print "What year?"
year = gets.chomp!
print "What number month?"
month = gets.chomp!
print "What number day?"
day = gets.chomp!
puts "#{day}/#{month}/#{year}"
I'm new to ruby but I think this should work.
Hi I have define this method
def change_date
date = Date.today
start_date = date.change(year: 2015, month: (2 * 3)).at_beginning_of_quarter
p 'aaaaaa'
p start_date
end
give me invalid date error .change is not working or am I doing it in a wrong way please guide me how to solve this. Thanx in advance.
This is because the month you are specifying doesn't have the current day.
I mean the current month (July) has 31 days but the month you're setting (June) has only 30 days. You can change your code like so:
# in Rails:
date = Date.today.beginning_of_month # or Date.today.change(day: 1)
Then chain your 'change' in front of the date variable.
This actually happens, because today is the 31 of July, and not all months have 31 days in it, for example June, the 6th month, has only 30 days in it.
I have the following piece of code:
humans = user.humans.joins(:human_logins).where(human_logins_count: 10).group('humans.id').having('MAX(human_logins.created_at) >= ?', Date.today() - schedule.value.day)
The problem with this is the created_at) >= ?segment as if my date is equal to 7 days ago, it will find the records that were made in the last 7 days, as opposed to finding the records that were created literally 7 days ago, not 6, not 9, exactly 7 days ago.
How can I make it so that its finding records created exactly 7 days ago? I was thinking of using something like ("? <= created_at AND created_at <= ?", schedule.value.days.ago.beginning_of_day, schedule.value.days.ago.end_of_day)but I'm not sure how I'd use it in this scenario.
How about this,
.group('human_logins.created_at').having('human_logins.created_at = ?', Date.today() - 7)
.having(created_at: schedule.value.days.ago.beginning_of_day.. schedule.value.days.ago.end_of_day )
I'd like to Post's to be displayed either showing how many minutes ago they were posted or weeks ago.
For example, if the post was created 5 minutes ago, it should be displayed as
5m
5 days ago:
5d
10 weeks ago:
10w
and so on.
Previously I was just using time_ago_in_words, but would prefer a simpler view.
Check out strftime, it will probably help you.
If it doesn't, take a look at to_formatted_s.
Hope it helps =]
Looks like you need to create your own solution, for example:
In your ApplicationHelper:
def short_time_ago_in_words(time)
case time
when (1.minute.ago)...(Time.now) then "1m"
when...
when (Date.today - 1)..(Date.today) then "1d"
when (Date.today - 7)..(Date.today) then "1w"
when...
end
end