Formatting a date - Rails [duplicate] - ruby-on-rails

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.

Related

Rails find someones birthday [duplicate]

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.

Swift Date object with different format [duplicate]

This question already has answers here:
Comparing NSDates without time component
(17 answers)
Closed 5 years ago.
I've got a Date object that holds year/month/day and hour/minute/second data. I need to drop the hour/minute/second part as it's making comparing days problematic.
The issue is that the only obvious way I see of doing it is turning the day into a String and then using a DateFormatter with yyyy/mm/dd format to turn it BACK into a Date. This seems like a waste, is it really the only way?
Thanks a lot.
Never convert dates to String and back to Date.
You are looking for the startOfDay function of Calendar:
let date = Date()
let startOfDay = Calendar.current.startOfDay(for: date)

Loop over hour:minute [duplicate]

This question already has answers here:
How can i loop through a daterange with different intervals?
(5 answers)
Closed 8 years ago.
I'm with Rails 4:
I need to make a loop in a table to display time of the day as title of column, like that
8:00 | 8:30 | 9:00 .... 20:00 | 20:30 | 21:00
Do you have any idea how to define the loop?
I've try complicated things with step() but meaby I have miss a easy way using Time?
Thanks
You could use Rails DateTime like this (for n steps you want)
startdate = DateTime.new(2001,2,3)
interval = 30
formatstr = '%H:%M'
(0..n).map{|offset| startdate + (offset * interval).minutes }
.map{|date| date.strftime(formatstr)}

How to correctly get the days for "posted X days ago" for a blog post date [duplicate]

This question already has answers here:
Number of days between two NSDates [duplicate]
(16 answers)
Relative string from NSDate
(5 answers)
Closed 8 years ago.
So I'm trying to get the amount of days a blog post was posted ago. The end result would be something like "posted X days ago."
After spending some time on the problem, I figured it out by getting the NSTimeInterval, dividing to get days and then rounding. Although I got the output I wanted, I feel I am doing it wrong or there is a much more straight forward way of doing it.
tempDate is a NSDate object of when the blog was posted.
NSTimeInterval timeSince = [tempDate timeIntervalSinceNow];
timeSince = timeSince/60/60/24*-1; // seconds to days
int daysSince = lroundf(timeSince);
you could use NSCalendar and NSDateComponent to get the number of days (and you don't even need to convert your tempDate)... See the good answer given here Number of days between two NSDates

Convert week number to a date [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I get a Date from a week number?
Finding the date for a given week number
How would I convert a week number (%W) back to a certain date. For example converting week 20 to May 15, 2012.
Check Date#commercial:
Date.commercial(2012, 20)
#=> Mon, 14 May 2012

Resources