LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Report Post  
Posted to microsoft.public.excel.newusers
external usenet poster
 
Posts: 1
Default Formula needed to calculate difference between Two dates andtimes

Hi,

Here's a function I wrote which displays the time in W days, X hours,
Y minutes and Z seconds. You simply need to set the start date+time,
and the end date + time.

Mark.




Public Function TimeDif(StartTime As Date, EndTime As Date) As String

'################################################# ######################
'################################################# ######################
'####
'#### This function returns a string expression of the number of
'#### days, hours, minutes, and seconds between two dates.
'####
'#### Code written by Mark Geels 10/09/2008
'#### Please retain this
heading
'################################################# ######################
'################################################# ######################


'Define constants
Const HPD = 24, MPD = 1440, SPD = 86400 'hours / minutes /
seconds per day
Const HPC = (1 / HPD), MPC = (1 / MPD), SP = (1 / SPD) 'one
unit (Hour Min Sec) as a percentage of one day

'Define variables
Dim Days, Hours, Minutes, Seconds As Integer
Dim dblTimeDif As Double
Dim strDays, strHours, strMinutes, strSeconds As String


dblTimeDif = EndTime - StartTime


'Set Days
Days = Int(dblTimeDif)
dblTimeDif = dblTimeDif - Days
If Days = 0 Then
strDays = ""
Days = ""
Else: strDays = " Days, "
End If

'Set Hours
Hours = Int(dblTimeDif * HPD)
dblTimeDif = dblTimeDif - (Hours * HPC)
If Days = "" And Hours = 0 Then
strHours = ""
Hours = ""
Else: strHours = " Hours, "
End If

'Set Minutes
Minutes = Int(dblTimeDif * MPD)
dblTimeDif = dblTimeDif - (Minutes * MPC)
If Days = "" And Hours = "" And Minutes = 0 Then
strMinutes = ""
Minutes = ""
Else: strMinutes = " Minutes, and "
End If

'Set Seconds
Seconds = Round(dblTimeDif * SPD, 0)
If Days = "" And Hours = "" And Minutes = "" And Seconds = 0
Then
strSeconds = ""
Seconds = ""
Else: strSeconds = " Seconds"
End If

TimeDif = Days & strDays & Hours & strHours & Minutes & strMinutes
& Seconds & strSeconds


End Function
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formula to calculate the difference in weeks between two dates IGG Excel Worksheet Functions 3 March 10th 08 11:50 AM
calculate difference between two times Ken Excel Worksheet Functions 6 July 21st 06 09:05 PM
formula to calculate difference between dates and times Breezie Excel Discussion (Misc queries) 2 February 25th 06 04:14 PM
NEED A FORMULA TO CALCULATE DIFFERENCE BETWEEN TWO TIMES IN 24HR . teezee New Users to Excel 2 January 7th 06 10:34 PM
Formula to calculate elapsed time between certain dates and times Stadinx Excel Discussion (Misc queries) 6 March 25th 05 07:02 AM


All times are GMT +1. The time now is 09:49 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"