G
Greg Maxey
I need a smart guy or gal to help me regain my sanity. I pulled a macro off
Google that would calculate a person's age based on a today's date and their
birthdate. I am setting two date variables with a calendar control
(stripped out here) and I am trying to extend the macro to calculate down to
the hours and minutes.
At first glance it look easy enough, but I started hitting walls
(figuratively and litterally) when my test involved spans over a day. For
example the code would always return 1 Day even if the the variables where
01/24/05 23:59 and 01/25/05 00:01. While that is a day when generally
speaking of someone's age, it is more like, actually just like, 2 minutes.
I finally put together the following, which I think is working. That only
means that I haven't found a way to break it. The output is rather crude,
but you can get the jist of it. I would appreciate any feedback or comments
on any shortcomings any of you may see or suggestions for improvement. I
feel like that there is a complex formula similiar to the years, months, and
days portition which is someone else's work, but I can figure it out.
Sub CalcTimeSpan()
Dim Years As Integer
Dim Months As Integer
Dim Days As Integer
Dim DaysInMonth As Integer
Dim Hours As Integer
Dim Minutes As Integer
Dim FirstDate As Date
Dim SecondDate As Date
FirstDate = "01/24/05 21:00"
SecondDate = Date
If SecondDate < FirstDate Then
MsgBox "Set the earlier date as the first anchor date"
End If
On Error GoTo ExitSub
If (Month(SecondDate) = 2) Then
DaysInMonth = 28 + (Month(SecondDate) = 2) * ((Year(SecondDate) Mod 4 = 0)
_
+ (Year(SecondDate) Mod 400 = 0) - (Year(SecondDate) Mod 100 =
0))
Else
DaysInMonth = 31 - (Month(SecondDate) = 4) - (Month(SecondDate) = 6) _
- (Month(SecondDate) = 9) - (Month(SecondDate) = 11)
End If
Years = Year(SecondDate) - Year(FirstDate) + (Month(SecondDate) _
< Month(FirstDate)) + (Month(SecondDate) = Month(FirstDate)) _
* (Day(SecondDate) < Day(FirstDate))
Months = (12 + Month(SecondDate) - Month(FirstDate) + (Day(SecondDate) _
< Day(FirstDate))) Mod 12
Days = (DaysInMonth + Day(SecondDate) - Day(FirstDate)) Mod DaysInMonth
If Hour(SecondDate) < Hour(FirstDate) Then
Hours = Hour(SecondDate) - Hour(FirstDate) + 24
Else: Hours = Hour(SecondDate) - Hour(FirstDate)
End If
Minutes = Minute(SecondDate) - Minute(FirstDate)
If Hour(SecondTime) < Hour(FirstTime) Then Days = Days - 1
MsgBox Years & Months & Days & Hours & Minutes
ExitSub:
End Sub
Google that would calculate a person's age based on a today's date and their
birthdate. I am setting two date variables with a calendar control
(stripped out here) and I am trying to extend the macro to calculate down to
the hours and minutes.
At first glance it look easy enough, but I started hitting walls
(figuratively and litterally) when my test involved spans over a day. For
example the code would always return 1 Day even if the the variables where
01/24/05 23:59 and 01/25/05 00:01. While that is a day when generally
speaking of someone's age, it is more like, actually just like, 2 minutes.
I finally put together the following, which I think is working. That only
means that I haven't found a way to break it. The output is rather crude,
but you can get the jist of it. I would appreciate any feedback or comments
on any shortcomings any of you may see or suggestions for improvement. I
feel like that there is a complex formula similiar to the years, months, and
days portition which is someone else's work, but I can figure it out.
Sub CalcTimeSpan()
Dim Years As Integer
Dim Months As Integer
Dim Days As Integer
Dim DaysInMonth As Integer
Dim Hours As Integer
Dim Minutes As Integer
Dim FirstDate As Date
Dim SecondDate As Date
FirstDate = "01/24/05 21:00"
SecondDate = Date
If SecondDate < FirstDate Then
MsgBox "Set the earlier date as the first anchor date"
End If
On Error GoTo ExitSub
If (Month(SecondDate) = 2) Then
DaysInMonth = 28 + (Month(SecondDate) = 2) * ((Year(SecondDate) Mod 4 = 0)
_
+ (Year(SecondDate) Mod 400 = 0) - (Year(SecondDate) Mod 100 =
0))
Else
DaysInMonth = 31 - (Month(SecondDate) = 4) - (Month(SecondDate) = 6) _
- (Month(SecondDate) = 9) - (Month(SecondDate) = 11)
End If
Years = Year(SecondDate) - Year(FirstDate) + (Month(SecondDate) _
< Month(FirstDate)) + (Month(SecondDate) = Month(FirstDate)) _
* (Day(SecondDate) < Day(FirstDate))
Months = (12 + Month(SecondDate) - Month(FirstDate) + (Day(SecondDate) _
< Day(FirstDate))) Mod 12
Days = (DaysInMonth + Day(SecondDate) - Day(FirstDate)) Mod DaysInMonth
If Hour(SecondDate) < Hour(FirstDate) Then
Hours = Hour(SecondDate) - Hour(FirstDate) + 24
Else: Hours = Hour(SecondDate) - Hour(FirstDate)
End If
Minutes = Minute(SecondDate) - Minute(FirstDate)
If Hour(SecondTime) < Hour(FirstTime) Then Days = Days - 1
MsgBox Years & Months & Days & Hours & Minutes
ExitSub:
End Sub