Get time between two DateTime values in hours (and minutes if simple)

510 views
Skip to first unread message

Ian Butterworth

unread,
Aug 14, 2015, 7:41:04 PM8/14/15
to julia-users
Trying to get the number of hours between these two dates (ideally "x hours and y minutes"), but can't figure out how to convert the duration variable into hours. The bottom line currently errors

timein = "2015/8/13 10:19:50"
timein2 = "2015/8/14 13:12:34"

time_series[1] = DateTime(timein,"yyyy/mm/dd HH:MM:SS")
time_series[2] = DateTime(timein2,"yyyy/mm/dd HH:MM:SS")

duration = time_series[2]-time_series[1]
Dates.Hour(duration)

Jeffrey Sarnoff

unread,
Aug 14, 2015, 8:01:05 PM8/14/15
to julia-users
Dates.Hour, .. Dates.Millisecond  what to put their parens around DateTime entities only.
Dates.Year. .. Dates.Day are more open-minded, enparenning DateTime or Date entities.

take 'em for a spin like this:
     Dates.Hour(time_series[2]) - Dates.Hour(time_series[1])

Here's to  a Good Evening

Ian Butterworth

unread,
Aug 15, 2015, 1:39:05 AM8/15/15
to julia-users
I took 'em for a spin, but your approach doesn't have legs if the datetimes in time_series come from different days. I.e. Dates.Hour just returns the hour in that day.

I can imagine a long-winded solution where the relevant time units are extracted and differenced, but I was hoping for simpler..

Good evening to you too

Jeffrey Sarnoff

unread,
Aug 15, 2015, 2:21:04 AM8/15/15
to julia-users
well that's accurate -- I was not trying to make them nefarious, I mistook the emphasis.
I will come back with a more fully driveable example in about 15mins.

On Friday, August 14, 2015 at 7:41:04 PM UTC-4, Ian Butterworth wrote:

Jeffrey Sarnoff

unread,
Aug 15, 2015, 2:46:04 AM8/15/15
to julia-users
Ian -- 

 "I can imagine a long-winded solution where the relevant time units are extracted and differenced, but I was hoping for simpler.." -- as you should!  

When I saw you use Hour in the first example, I thought you were doing some thing where hour counts were the focus ... (I will prepare a more  fully helpful example).

Jeffrey Sarnoff

unread,
Aug 15, 2015, 5:57:23 AM8/15/15
to julia-users

Good morning, .. and 

I expected that would work .. that the period operators would cooperate differently.
You deserve a more satisying introduction to "basic stuff with time intervals.
   ---  You want time differences to just work so you can do the same.
I agree.

Kaj Wiik

unread,
Aug 15, 2015, 5:59:36 AM8/15/15
to julia-users
You can get Int64 value by duration.value and convert 'by hand' from there. A possibility to convert from Millisecond to DateTime would be nice...
 

Jeffrey Sarnoff

unread,
Aug 15, 2015, 6:11:19 AM8/15/15
to julia-users


On Saturday, August 15, 2015 at 5:59:36 AM UTC-4, Kaj Wiik wrote:
You can get Int64 value by duration.value and convert 'by hand' from there. A possibility to convert from Millisecond to DateTime would be nice...
 

There should be
  
     durationAB = DateTimeB - DateTimeA
         rational_hours = convert(Hour, durationAB)
         

Jeffrey Sarnoff

unread,
Aug 15, 2015, 6:16:09 AM8/15/15
to julia-users
What would like two lines of code to do with durations?


On Friday, August 14, 2015 at 7:41:04 PM UTC-4, Ian Butterworth wrote:

Ian Butterworth

unread,
Aug 15, 2015, 12:59:27 PM8/15/15
to julia-users
Thanks guys. I ended up using Kaj's approach. Functionality like we discussed would be good if possible.

Jacob Quinn

unread,
Aug 15, 2015, 1:01:20 PM8/15/15
to julia...@googlegroups.com
Yeah, it's forthcoming. I left it out originally just to be conservative in code and function, but it's come up enough that we should add it in for TimePeriods. A good "up for grabs" kind of PR if anyone's feeling up for it.

-Jacob
Reply all
Reply to author
Forward
Message has been deleted
0 new messages