How to compare times in different time zones in Ruby? - ruby

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

Related

combine Date and Time into a DateTime from api response

What is the efficient way to combine Date and Time (strings) into a single DateTime? i am using football-api in response i am getting time attribute in "08:50" these format and date attribute "01.01.2018" these format. I want to save in database 2018-01-01 08:50:00 format in date field.
a = match["formatted_date"].to_date.strftime("%Y, %m, %d")
y = a[0..3].to_i
m = a[6..7].to_i
d = a[10..11].to_i
date = Date.new(y, m, d).to_datetime + Time.parse(match["time"]).seconds_since_midnight.seconds

[ruby]Get file, parse text and create date object

I have a raw .txt file formatted as such:
01.01.2017;New Year
16.04.2017;Easter
25.12.2017;Christmas
(Sidenote: dates are formatted as dd.mm.yyyy)
I'm trying to read this file, slice the text per line and make a hash out of it, with the key being the date, and its value the name of the corresponding public holiday.
I've already gotten so far:
holidays = Hash[*File.read('holidays.txt').split(/;|\n/)]
This results in the dates being set as strings, not date objects.
Any ideas as to how I could then transform these strings to Date (or DateTime) objects?
P.S.: I'm only using Ruby, so no Rails helpers...
Something like this
holidays = File.read('holidays.txt').split(/\n/).map do |row|
date, holiday_name = row.split(';')
date = Date.parse(date, '%d.%m.%Y')
[date, holiday_name]
end.to_h
=> {
#<Date: 2017-01-01 ((2457755j,0s,0n),+0s,2299161j)> => "New Year",
#<Date: 2017-04-16 ((2457860j,0s,0n),+0s,2299161j)> => "Easter",
#<Date: 2017-12-25 ((2458113j,0s,0n),+0s,2299161j)> => "Christmas"
}

Get difference between two dates in days using ruby

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

sorting based on date and time in flex

here i used the sort function for date and time,but it's only sort on date not time,i want sort based on both date and time:
private function dregdate_sortCompareFuction(itemA:Object, itemB:Object):int {
var dt1:Date = DateField.stringToDate(String(itemA.sbatchregdate),"DD/MM/YYYY");
var dt2:Date = DateField.stringToDate(String(itemB.sbatchregdate),"DD/MM/YYYY");
var dateA:Date = new Date(dt1);
var dateB:Date = new Date(dt2);
return ObjectUtil.dateCompare(dateA, dateB);}
kindly please share your knowledge.
thanks
As you can see by the format you're passing, DateField.stringToDate only cares about dates and not times.
You could use Date.parse if your input string was in one of these formats:
MM/DD/YYYY HH:MM:SS TZD
HH:MM:SS TZD Day Mon/DD/
Mon DD YYYY HH:MM:SS
Day Mon DD HH:MM:SS TZD YYYY
Day DD Mon HH:MM:SS TZD YYYY
Mon/DD/YYYY HH:MM:SS TZD
YYYY/MM/DD HH:MM:SS TZD
but yours is not.
So give this, i'd use a Regular Expression to extract the individual values from the string and then build the Date object manually, something like:
var regex:RegExp = /^(\d+)\/(\d+)\/(\d+) (\d+):(\d+)$/i;
var data:Object = regex.exec(itemA.sbatchregdate);
var dateA:Date = new Date(o[3], o[2]-1, o[1], o[4], o[5]);
once you have the two dates created with the proper time values, the comparison should work as expected.

How to get a date from day number and year?

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)>

Resources