Calculating time differences?

A

Angyl

How would I get Word to calculate time as in a time sheet, such as:

In Time 8:00AM
Out Time 12:30PM

Word would give me 4.5 from that...
 
J

Jay Freedman

How would I get Word to calculate time as in a time sheet, such as:

In Time 8:00AM
Out Time 12:30PM

Word would give me 4.5 from that...

Use the DateDiff function to get the difference. Since the function is
defined to return a Long (a large integer value) rather than a Single
or a Double, you can't use the "h" interval parameter if you need to
calculate fractions of an hour; you have to use the "n" parameter to
get minutes, and then compute hours:minutes from that number...

Sub demo()
Dim startTime As Date, endTime As Date
Dim diffHr As Long, diffMin As Long

startTime = CDate("8:00 am")
endTime = CDate("12:35 pm")
diffMin = Abs(DateDiff("n", startTime, endTime))
diffHr = Int(diffMin / 60) ' truncated to whole hours
diffMin = diffMin Mod 60 ' remaining minutes
MsgBox diffHr & ":" & diffMin
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top