| Difference between dates in second, minutes, hours, days |
|
|
|
| Tutorials | |||
| Written by Administrator | |||
|
The code below tells you the time difference between two dates. 1 Function TimeDifferenceString(ByVal dateStart As Date, ByVal dateEnd As Date, Optional ByVal blPad As Boolean = True, Optional ByVal intMinPlace As Integer = 0) As String 2 3 '------------------------------------------------------------------------------ 4 ' Given two Date variables, return a string that holds the difference between 5 ' the date/times in days, hours, minutes, and seconds, formatted as 6 ' "#d-#h:#m:#s". 7 ' ------------------------------------------------------------------------------ 8 ' Arguments: 9 ' dateStart The first (or start) date/time. 10 ' dateEnd The first (or end) date/time. 11 ' blPad True: Pad each # (day, hour, min, sec) to a minimum of two digits, with a leading 0. 12 ' False: Do not pad #s. 0 will still be returned as "0". 13 14 ' intMinPlace What place (if any) to build the string out to even 15 ' if that place (and all higher places) have a value of 0. 16 ' <0 : do not automatically include any places; a time 17 18 ' difference of 0 will result in an empty return string 19 20 ' 0 : always include seconds; a time difference of 0 will 21 22 ' result in a return string of "0s" (or "00s", if 23 24 ' blPad is True) 25 26 ' 1 : always include minutes: a time difference of 0 27 28 ' minutes will result in a return string of "0m:#s" 29 30 ' (or "00m:##s", if blPad is True) 31 32 ' 2 : always include hours: a time difference of 0 hours 33 34 ' will result in a return string of "0h:#m:#s" (or 35 36 ' "00h:##m:##s", if blPad is True) 37 38 ' >=3 : always include days: a time difference of 0 days 39 40 ' will result in a return string of "0d-#h:#m:#s" (or 41 42 ' "0d-##h:##m:##s", if blPad is True) 43 44 Dim intTemp As Integer 45 Dim strFormat As String 46 Dim intSecond As Integer 47 Dim intMinute As Integer 48 Dim intHour As Integer 49 Dim intDay As Integer 50 Dim blCarry As Boolean 51 52 strFormat = String(IIf(blPad, 2, 1), "0") 53 54 ' ----- calculate seconds ----- 55 56 intSecond = Round((dateEnd - dateStart) * 24 * 60 * 60) Mod 60 57 58 intTemp = Int((dateEnd - dateStart) * 24 * 60 * 60) Mod 60 59 60 blCarry = ((intSecond <> intTemp) And (intSecond = 0)) 61 62 ' ----- calculate minutes ----- 63 64 intMinute = (Int((dateEnd - dateStart) * 24 * 60) + IIf(blCarry, 1, 0)) Mod 60 65 66 67 blCarry = ((intMinute = 0) And blCarry) 68 69 ' ----- calculate hours ----- 70 71 intHour = (Int((dateEnd - dateStart) * 24) + IIf(blCarry, 1, 0)) Mod 24 72 73 74 blCarry = ((intHour = 0) And blCarry) 75 76 77 78 ' ----- calculate days ----- 79 80 intDay = Int(dateEnd - dateStart) + IIf(blCarry, 1, 0) 81 82 ' ----- build return string ----- 83 84 ' include days place if value is non-zero or intMinPlace specifies days 85 86 If ((intDay <> 0) Or (intMinPlace >= 3)) Then 87 88 TimeDifferenceString = Format(intDay, strFormat) & "d-" 89 90 End If 91 92 93 ' include hours place if value is non-zero or intMinPlace specifies hours 94 95 If ((intHour <> 0) Or (intMinPlace >= 2)) Then 96 97 TimeDifferenceString = TimeDifferenceString & Format(intHour, strFormat) & "h:" 98 99 End If 100 101 102 ' include minutes place if value is non-zero or intMinPlace specifies 103 104 ' minutes 105 106 If ((intMinute <> 0) Or (intMinPlace >= 1)) Then 107 108 TimeDifferenceString = TimeDifferenceString & Format(intMinute, strFormat) & "m:" 109 110 End If 111 112 ' include seconds place if value is non-zero or intMinPlace specifies 113 114 ' seconds 115 116 If ((intSecond <> 0) Or (intMinPlace >= 0)) Then 117 118 TimeDifferenceString = TimeDifferenceString & Format(intSecond, strFormat) & "s" 119 120 End If 121 122 End Function 123 124 125 Sub TestFunction() 126 'Test Function returns 20ds-00m:00s 127 MsgBox(TimeDifferenceString("12/12/09", "01-01-10", True, 1)) 128 End Sub
|



Comments
RSS feed for comments to this post