Date and Time difference (1 Viewer)

Smart

Registered User.
Local time
Today, 22:35
Joined
Jun 6, 2005
Messages
436
I have four fields

Date Sent (date) Time Sent (Time) Date Recvd (date) Time Rcvd (time)

I want to know how many hours have elapsed between date and time sent and date and time rcvd


Any Ideas please as I am having a blonde moment
 

Brianwarnock

Retired
Local time
Today, 22:35
Joined
Jun 2, 2003
Messages
12,701
Datediff("h",date1,date2)

Brian

Edit Just noticed that you have separate fields for date and time so some further arithmetic will be needed to sum both, hum the sent time could be less than the rcved time, not sure now.
 
Last edited:

Brianwarnock

Retired
Local time
Today, 22:35
Joined
Jun 2, 2003
Messages
12,701
Ok quick test shows not a problem

Datediff("h",datesent,datercvd)+Datediff("h",timesent,timercvd)

Brian
 

namliam

The Mailman - AWF VIP
Local time
Today, 23:35
Joined
Aug 11, 2003
Messages
11,695
Well allmost I think Brian...

datediff("H", [Date Sent]+[Time Sent], [Date Recvd]+[Time Rcvd])

Edit:
Just want to add that one should not use spaces or special characters in colum and/or table names.
 

Brianwarnock

Retired
Local time
Today, 22:35
Joined
Jun 2, 2003
Messages
12,701
Yes both syntax work, agree about spaces to the extent that give or take a typo I omitted them:D

Brian
 

Jon K

Registered User.
Local time
Today, 22:35
Joined
May 22, 2002
Messages
2,209
It depends on the results you want and the length of elapsed time.


DatePart("h",([Date Recvd]+[Time Recvd])-([Date Sent]+[Time Sent]))
- returns an integer between 0 and 23, representing the number of completed hours. It works correctly when elapsed time is less than 24 hours.

(([Date Recvd]+[Time Recvd])-([Date Sent]+[Time Sent]))*24
- returns elapsed time in numeric hours with several decimal places. It always works correctly.

Format((([Date Recvd]+[Time Recvd])-([Date Sent]+[Time Sent])),"hh:nn:ss")
- returns elapsed time in Hours:Minutes:Seconds in a text string. It works correctly when elapsed time is less than 24 hours.

Int((([Date Recvd]+[Time Recvd])-([Date Sent]+[Time Sent]))*24) & ":" & Format((([Date Recvd]+[Time Recvd])-([Date Sent]+[Time Sent])),"nn:ss")
- returns elapsed time in Hours:Minutes:Seconds in a text string. It works if elapsed time may be 24 hours or over.
.
 

EMP

Registered User.
Local time
Today, 22:35
Joined
May 10, 2003
Messages
574
DateDiff("h") doesn't work correctly all the time.

For example, DateDiff("h") of 1/10/2007 3:59:59 AM and 1/10/2007 4:00:00 AM returns 1 hour though actually only one second has elapsed.

^
 

boblarson

Smeghead
Local time
Today, 14:35
Joined
Jan 12, 2001
Messages
32,059
EMP is correct and that's why for time differences I will usually go with minutes or seconds and then do calculations for hours. The same actually goes for years too.
 

namliam

The Mailman - AWF VIP
Local time
Today, 23:35
Joined
Aug 11, 2003
Messages
11,695
EMP said:
DateDiff("h") doesn't work correctly all the time.

For example, DateDiff("h") of 1/10/2007 3:59:59 AM and 1/10/2007 4:00:00 AM returns 1 hour though actually only one second has elapsed.

^
This seems only logical as you are asking the difference in hours. This rounds UP allways...

Jon K said:
DatePart("h",([Date Recvd]+[Time Recvd])-([Date Sent]+[Time Sent]))
- returns an integer between 0 and 23, representing the number of completed hours. It works correctly when elapsed time is less than 24 hours.
Sorry not true...
x = now()
y = now + 1.1
?datediff("H",x,y)
26
?datediff("H",y,x)
-26

Jon K said:
Int((([Date Recvd]+[Time Recvd])-([Date Sent]+[Time Sent]))*24) & ":" & Format((([Date Recvd]+[Time Recvd])-([Date Sent]+[Time Sent])),"nn:ss")
This one offcourse is best :D but this the OP didnt ask....
 

EMP

Registered User.
Local time
Today, 22:35
Joined
May 10, 2003
Messages
574
Originally Posted by namliam

Sorry not true...
x = now()
y = now + 1.1
?datediff("H",x,y)
26
?datediff("H",y,x)
-26
I think you need to re-read Jon's statement:-

DatePart("h",([Date Recvd]+[Time Recvd])-([Date Sent]+[Time Sent]))
- returns an integer between 0 and 23, representing the number of completed hours. It works correctly when elapsed time is less than 24 hours.


Jon was talking about DatePart, not DateDiff.
And when 1.1 was added to Now(), the time elapsed was over 24 hours.

I think when Jon said: "It depends on the results you want and the length of elapsed time.", he was referring to the OP's question.


Originally Posted by namliam

This seems only logical as you are asking the difference in hours. This rounds UP allways...
As for the logic of rounding UP 1 second to 1 hour, I have no comment.

But if you try DateDiff("H") on 1/10/2007 3:00:00 AM and 1/10/2007 4:30:00 AM, you will see that it rounds DOWN, not UP.

^
 
Last edited:

Brianwarnock

Retired
Local time
Today, 22:35
Joined
Jun 2, 2003
Messages
12,701
EMP said:
DateDiff("h") doesn't work correctly all the time.

For example, DateDiff("h") of 1/10/2007 3:59:59 AM and 1/10/2007 4:00:00 AM returns 1 hour though actually only one second has elapsed.

^

Been out of the loop too long, forgot about datediffs penchant for rounding up, and my test was too simplistic to catch it. apologies to Smart

Brian
 

Jon K

Registered User.
Local time
Today, 22:35
Joined
May 22, 2002
Messages
2,209
My two cents

DateDiff never rounds. It strips except for DateDiff("s").

So DateDiff("n") strips the seconds.
DateDiff("h") strips the minutes and seconds.
DateDiff("d") strips the hours, minutes and seconds.
etc.
.
 
Last edited:

Brianwarnock

Retired
Local time
Today, 22:35
Joined
Jun 2, 2003
Messages
12,701
The important point is that it strips before the subtraction takes place, otherwise it would be the eqivalent to rounding down, doing it the way it does is equivalent to rounding up.

Just my 2 pence worth.

Brian
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:35
Joined
Feb 28, 2001
Messages
27,317
If date/time received and date/time sent were one field, you could just subtract the fields and get the difference in days and fractions thereof. OK, if you wanted something fancy for the difference, you might have to work on a formatter or something like that.

If you have date and time in separate fields, you are asking for a harder time than you need. Rethink combining them.
 

namliam

The Mailman - AWF VIP
Local time
Today, 23:35
Joined
Aug 11, 2003
Messages
11,695
I agree combining the fields saves space (not much of an issue ussualy) but makes for easier access to your data. Unless there is a major requirement for this... I too would join the 2 fields.
 

Users who are viewing this thread

Top Bottom