How can I create DateTime object based on the day? - ruby

I want to create an Datetime object based on the number of day in the year.
This number is from the 365 days of the year (for example it can be: 123 or 23 or 344...)
How can I do that?
Thanks

Use the DateTime.ordinal method. Here's an example to get the 100th day of year 2011.
require 'date'
year, day = 2011, 100
DateTime.ordinal(year, day)
# #<DateTime: 2011-04-10T00:00:00+00:00 (4911323/2,0,2299161)>

If you want it as the number of days from now you should do the following:
time = Time.new + (60*60*24)*(numberOfDaysFromNow)
If you want it as the number of days from the start of the year you should do the following
time = Time.new(Time.now.year) + (60*60*24)*(dayOfTheYear-1)

Related

Ruby on Rails select between start of day and middle day from lessons

Hello im developing an app for a gym and in the stats section i got stucked in this query. I need to select all the lessons that starts between the start of the day and 13:00 from the database and then count them but i havent figure out how to make the sentence. the closest i did was this, How can i select all the lessons that starts before 13:00 of all month togheter? thanks
#lessons_morning = Lesson.where(start_date: (beginning_of_day)..Time.beginning_of_day + 13.hours,updated_at: (Time.now.beginning_of_month)..Time.now.end_of_day)
Since you are storing both date and time in the same column, I would do it like this:
First, let's query for all lessons in the current month:
# use Time.zone.now instead of Time.now to use the timezone specified in your application config
#lessons = Lesson.where(
start_date: Time.zone.now.beginning_of_month..Time.zone.now.end_of_month
)
Then, let's only find lessons which start between 0:00 and 13:00:
#lessons.select do |lesson|
start_hour = lesson.start_date.strftime('%H%M').to_i
start_hour <= 1300
}

How to find the date number given the Year, month, day of the week and the week it falls on in ruby

So i'm new to ruby and not to familiar with the syntax of the Date gem, but is there a way to find the date of a moving holiday like fathers day?
So my current class for the moving holidays looks like this:
class MovingHoliday < Holiday
def initialize (name, month, day_of_week, week, year=2016)
#name = name
#month = month
#day_of_week = day_of_week
#week = week
#year = year
#my_date = Date.new(year, month, day)
end
end
and my input looks like this:
fathers_day = MovingHoliday.new("Fathers Day", 6, "sunday", 4, year)
The 6 is the month, "sunday" is well the day_of_the_week, and 4 is the week it falls on.
I just can't figure out the syntax if there is any that would/could do conversion for this.
And I can't use any gems other then rubys Date.
class MovingHoliday < Holiday
def initialize (name, month, day_of_week, week, year=2016)
#name = name
#month = month
#day_of_week = day_of_week
#week = week
#year = year
#my_date = Date.new(year, month, 1)
#my_date += 1 while !my_date.send("#{day_of_week}?")
#my_date += (7 * (week - 1))
end
end
Not pretty, but it works. It takes the first of the month, moves up a day until it's the correct day of the week, and then increases by the appropriate number of weeks.
As it stands, it assumes that you're given an actual day of the week. You'll probably want to sanitize the input in some manner or convert from "sunday" to 0 (my_date.wday returns 0 for sunday, 1 for monday, etc)
date in Ruby is not a gem, it's part of the standard library, it's just not loaded unless you require it. (In Ruby, require is used for both purposes, loading gems and loading parts of the standard library that are not loaded by default.)
I don't know of any built-in functionality that would calculate the date for you, I think you'd have to write that yourself, since you don't want to consider other gems.

List Array of Days Between Two Dates

I would like to list an array of days between two dates. I can list an array of months with the code below. How might I change this to show every day between two dates?
require 'date'
date_from = Date.parse('2011-05-14')
date_to = Date.parse('2011-05-30')
date_range = date_from..date_to
date_months = date_range.map {|d| Date.new(d.year, d.month, 1) }.uniq
date_months.map {|d| d.strftime "%d/%m/%Y" }
puts date_months
I don't know which day you meant, thus I have shown all the ways.
#wday is the day of week (0-6, Sunday is zero).
(date_from..date_to).map(&:wday)
#mday is the day of the month (1-31).
(date_from..date_to).map(&:mday)
#yday is the day of the year (1-366).
(date_from..date_to).map(&:yday)
OP's actual need was not much clear to me. After few comments between us, I came to know from OP's comment, the below answer OP is looking for -
(date_from..date_to).map(&:to_s)

Get the last day of the month given the month

How would I get the last day of a current month given the month number.
For example if the month is 8 (August 2014), how would I get the last day of the month? (The last day of August is the 31th). The function(8) would return 31.
Thank you for the help.
Using Date::civil:
require 'date'
Date.civil(2014, 8, -1).day
# => 31
According to the documentation:
The month and
the day of month should be a negative or a positive number (as a
relative month/day from the end of year/month when negative).
They should not be zero.
require 'date'
def function(month)
raise "invalid month" unless month.is_a?(Integer) and month.between?(1, 12)
month == 12 ? 31 : Date.new(Date.today.year,month+1,1).prev_day.day
end
function(8)
# => 31
source: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-i-prev_day

How can I compare a date field with date.today and display the differance?

I have a table of accounts, each with an expiration date. The expiration date is saved in a date field (day, month and year).
How can I compare this to the current date and display the number of days until the expiration date is reached?
Thanks for any help it's much appreciated!
simple (Date.today - account.expiration_date).to_i will give you the integer number - the difference in days :)
You can convert your components into a target date and use some of the methods in DateHelper to display a human readable distance:
target_date = Date.new(model.year, model.month, model.day)
distance_of_time_in_words(target_date, Date.today)
=> "4 months"

Resources