Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2007

Tip: Looking for answers? Try searching our database.

Calculating time differences?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Angyl - 16 Oct 2007 22:15 GMT
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...
Jay Freedman - 17 Oct 2007 00:59 GMT
>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

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.