I'm trying to convert a day number for a given year back into its date, i.e. the inverse of the method yday. For example, given the 200th day of the year 2012 I want to get the date 2012-07-18.
This is a core feature of Date and DateTime.
See http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime.html#method-c-ordinal
d = Date.ordinal( 2012, 200 )
=> #<Date: 2012-07-18 ((2456127j,0s,0n),+0s,2299161j)>
d = DateTime.ordinal( 2012, 200 )
=> #<DateTime: 2012-07-18T00:00:00+00:00 ((2456127j,0s,0n),+0s,2299161j)>
Related
I need to calculate how many days lived on earth based on three parameters : day, month and year.
I don't know how to convert the input onto a date; and then how to convert the date onto number of days.
This is my code for the moment...
require 'date'
def age_in_days(day, month, year)
lived = (day+"-"+month+"-"+year).to_s
date_lived Date.parse(lived)
return (Date.today - date_lived).to_i
end
You can create a Date object directly with:
Date.new(year, month, day)
There's no need to convert it to a string and parse. Parsing could also be dangerous. Is 1982-04-02 the 4th of February or 2nd of April?
You cannot add a number and string like this, BTW. Should 1 + '2' be '12' or 3? Ruby cannot decide for you so you need to explicitly convert integers to string.
day.to_s + "-" + month.to_s + "-" + year.to_s
or simply
[day, month, year].join('-')
But you don't need it anyway:
require 'date'
def age_in_days(day, month, year)
birthdate = Date.new(year, month, day)
(Date.today - birthdate).to_i
end
I have this date that which is in timezone offset format that I need to convert to UTC format.
For example:
date1 = 2017-07-13T17:13:12-04:00
date2_utc = 2017-07-13 21:13:12 UTC
I need to compare if those two date are same date. Or If I can convert date1 to UTC then i can compare those two.
I need to compare if those two date are same date.
You don't have to convert them, == will take care of the time zone:
t1 = Time.parse('2017-07-13T17:13:12-04:00')
#=> 2017-07-13 17:13:12 -0400
t2 = Time.parse('2017-07-13 21:13:12 UTC')
#=> 2017-07-13 21:13:12 UTC
t1 == t2
#=> true
In my Rails application, I need to find if a given date is the second or fourth Saturday of the month. What's the efficient way to do this? Is there a gem I can use?
Days 1 to 7 are week 0
Days 8 to 14 are week 1
Days 15 to 21 are week 2
Days 22 to 28 are week 3
To get the week id, we can calculate (date.day-1)/7. Since the id is zero-based, the second and fourth saturdays have an odd week id :
def second_or_fourth_saturday?(date)
date.saturday? && ((date.day - 1) / 7).odd?
end
The second Saturday has to be in the day range 8-14, and the fourth in the day range 22-28. So I think this should work
def second_or_forth_saturday?(date)
return false unless date.saturday?
(8..14).include?(date.day) || (22..28).include?(date.day)
end
def fourth_saturday?(date)
saturdays = (date.beginning_of_month..date.end_of_month).select { |date| date.wday == 6 }
[satudays.second, saturdays.fourth].include?(date)
end
create a month dates range
select Saturdays
see, if second or fourth Saturday is equal to date
As #Stefan kindly suggested, the first line of the method could be written as follows (using all_month):
saturdays = date.all_month.select(&:saturday?)
References:
I would like to know the time difference between two dates (Time/DateTime class) in days, i have one date created from the Date class and the other coming from my rails model, the one coming from the model is of the Time class, i want to know the number of days in between.
#current_day = Date.new
#created_day = establishment.created_at
i have tried getting this result using the days_ago function but it doesn't take into consideration the months.
old_date = Date.parse('2016-08-10')
new_date = Date.parse('2016-09-02')
days_between = (new_date - old_date).to_i
You have to convert days betweet to integer, because otherwise the result would be instance of Rational class.
Solution to your question after edit:
#current_day = Date.new
#created_day = establishment.created_at
days_between = (#current_day - #created_day.to_date).to_i
Solution to your question: Try this one it will work.
days_between = ("Tue Oct 24 09:20:25 UTC 2017".to_date.."Fri Oct 27 11:20:08 UTC 2017".to_date).count
If you print the result(days_between) you will get result as 4
Every 5 minutes a row in a sql server table is added. The fields are:
DateTime timeMark,Int total.
Using entity framework I want to populate a new list covering a whole week of five minute values using an average of the totals from the last three months.
How would I accomplish this with Entity Framework?
Assuming your log is really exact on the "five mintues", and that I understood well
, you want a list with 7 day * 24 hours * (60/5) minutes, so 2016 results ?
//define a startDate
var beginningDate = <the date 3 month ago to start with>;
//get the endDate
var endDate = beginningDate.AddMonths(3);
var list = myTable.Where(m => m.TimeMark >= beginningDate && m.TimeMark <=endDate)
//group by dayofWeek, hour and minute will give you data for each distinct day of week, hour and minutes
.GroupBy(m => new {
dayofWeek = SqlFunctions.DatePart("weekday", m.TimeMark),
hour = SqlFunction.DatePart("hour", m.TimeMark),
minute = SqlFunctions.DatePart("minute", m.TimeMark)
})
.Select(g => new {
g.Key.dayofWeek,
g.Key.hour,
g.Key.minute,
total = g.Average(x => x.Total)
});