Case "DDMMYYYY"
intYear = lngdate Mod 10000
intMonth = (lngdate \ 10000) Mod 100
intDay = lngdate \ 1000000
Case "YYMMDD", "YYYYMMDD"
intYear = lngdate \ 10000
intMonth = (lngdate \ 100) Mod 100
intDay = lngdate Mod 100
Case Else
fOk = False
End Select
If fOk Then
dhCNumdate = DateSerial(intYear, intMonth, intDay)
Else
dhCNumdate = Null
End If
End Function
Function dhCStrdate(strDate As String, Optional strFormat As String
"") As Date
' Given a string containing a date value, and a format
' string describing the information in the date string,
' convert the string into a real date value.
' From "VBA Developer's Handbook"
' by Ken Getz and Mike Gilbert
' Copyright 1997; Sybex, Inc. All rights reserved.
' In:
' strDate:
' String expression containing a date to be converted.
' strFormat (Optional):
' String expression containing a format specifier for the
' string in strdate. If omitted, the function uses "", which
' will cause it to use the Cdate function to attempt to
' conversion, just as it will if any other unknown format
' string is passed in.
'
' Allowable formats:
' "MMDDYY", "MMDDYYYY"
' "DDMMYY", "DDMMYYYY"
' "YYMMDD", "YYYYMMDD"
' "DD/MM/YY", "DD/MM/YYYY" ("/" stands for any delimite
in the date string)
' "YY/MM/DD", "YYYY/MM/DD"
' Out:
' Return Value:
' The value in strDate, converted to a date, if possible.
' Example:
' dhCStrdate("59/04/22", "YY/MM/DD") returns the real dat
#4/22/59#
'
Dim strYear As String
Dim strMonth As String
Dim strDay As String
Dim fDone As Boolean
Select Case strFormat
Case "MMDDYY", "MMDDYYYY"
strYear = Mid(strDate, 5)
strMonth = Left(strDate, 2)
strDay = Mid(strDate, 3, 2)
Case "DDMMYY", "DDMMYYYY"
strYear = Mid(strDate, 5)
strMonth = Mid(strDate, 3, 2)
strDay = Left(strDate, 2)
Case "YYMMDD"
strYear = Left(strDate, 2)
strMonth = Mid(strDate, 3, 2)
strDay = Right(strDate, 2)
Case "YYYYMMDD"
strYear = Left(strDate, 4)
strMonth = Mid(strDate, 5, 2)
strDay = Right(strDate, 2)
Case "DD/MM/YY", "DD/MM/YYYY"
strYear = Mid(strDate, 7)
strMonth = Mid(strDate, 4, 2)
strDay = Left(strDate, 2)
Case "YY/MM/DD"
strYear = Left(strDate, 2)
strMonth = Mid(strDate, 4, 2)
strDay = Right(strDate, 2)
Case "YYYY/MM/DD"
strYear = Left(strDate, 4)
strMonth = Mid(strDate, 6, 2)
strDay = Right(strDate, 2)
Case Else
' If none of the other formats were matched, just count o
Cdate
' to do the conversion. It may fail, but we can't help ou
here.
dhCStrdate = CDate(strDate)
fDone = True
End Select
If Not fDone Then
dhCStrdate = DateSerial(Val(strYear), Val(strMonth)
Val(strDay))
End If
End Function
Function DaysInMonth(Optional dtmDate As Date = 0) As Integer
If dtmDate = 0 Then
dtmDate = Date
End If
DaysInMonth = DateSerial(Year(dtmDate), Month(dtmDate) + 1, 1) - _
DateSerial(Year(dtmDate), Month(dtmDate), 1)
End Function
Function dhCountDOWInMonth(ByVal dtmDate As Date, _
Optional intDOW As Integer = 0) As Integer
' Calculate the number of specified days in
' the specified month.
'
' From "VBA Developer's Handbook"
' by Ken Getz and Mike Gilbert
' Copyright 1997; Sybex, Inc. All rights reserved.
'
' In:
' dtmDate:
' date value specifying the month and year
' If intDOW is missing, this date also
' supplies the day of week to count.
' intDOW: (Optional)
' If supplied, contains the day of week
' (vbSunday (1) - vbSaturday (7)) to be
' counted within the specified month/year.
' If not supplied, the function uses the
' day of week of the required date parameter.
' Out:
' Return value:
' The number of days matching intDOW (or dtmDate)
' in the specified month/year.
'
' Example:
' dhCountDOWInMonth(#11/96#, 6) returns 5
' (there were 5 Fridays in November 1996)
' dhCountDOWInMonth(#11/3/96#) returns 4
' (11/3/96 was a Sunday, and there were 4 Sundays in th
month)
' dhCountDOWInMonth(#11/3/96#, 6) returns 5
' (the intDOW parameter overrides the day portion of the
date)
Dim dtmFirst As Date
Dim intCount As Integer
Dim intMonth As Integer
If (intDOW < vbSunday Or intDOW > vbSaturday) Then
' Caller must not have specified DOW, or it
' was an invalid number.
intDOW = Weekday(dtmDate)
End If
intMonth = Month(dtmDate)
' Find the first day of the month
dtmFirst = DateSerial(Year(dtmDate), intMonth, 1)
' Move dtmFirst forward until it hits the
' matching day number.
Do While Weekday(dtmFirst) <> intDOW
dtmFirst = dtmFirst + 1
Loop
' Now, dtmFirst is sitting on the first day
' of the requested number in the month. Just count
' how many of that day type there are in the month.
intCount = 0
Do While Month(dtmFirst) = intMonth
intCount = intCount + 1
dtmFirst = dtmFirst + 7
Loop
dhCountDOWInMonth = intCount
End Function
Function dhAgeUnused(dtmBD As Date, Optional dtmDate As Date = 0) _
As Integer
' From "VBA Developer's Handbook"
' by Ken Getz and Mike Gilbert
' Copyright 1997; Sybex, Inc. All rights reserved.
Dim intAge As Integer
If dtmDate = 0 Then
' Did the caller pass in a date? If not, use
' the current date.
dtmDate = Date
End If
intAge = DateDiff("yyyy", dtmBD, dtmDate)
If dtmDate < DateSerial(Year(dtmDate), Month(dtmBD), Day(dtmBD))
Then
intAge = intAge - 1
End If
dhAgeUnused = intAge
End Function
Function dhAge(dtmBD As Date, Optional dtmDate As Date = 0) As Integer
' Calculate a person's age, given their birthdate and
' an optional "current" date.
' From "VBA Developer's Handbook"
' by Ken Getz and Mike Gilbert
' Copyright 1997; Sybex, Inc. All rights reserved.
' In:
' dtmBD:
' The birthdate (or any other anniversary date)
' dtmDate:
' The reference date. If omitted, the code uses today's
' date.
' Out:
' Return Value:
' The number of fulls years between dtmBD and dtmDate.
' Example:
' dhAge(#5/22/59#, #1/1/97#) returns 37, since the anniversary
' hasn't passed yet (dateDiff would return 38).
If dtmDate = 0 Then
' Did the caller pass in a date? If not, use
' the current date.
dtmDate = Date
End If
dhAge = DateDiff("yyyy", dtmBD, dtmDate) + _
(dtmDate < DateSerial(Year(dtmDate), Month(dtmBD), Day(dtmBD)))
End Function
Function dhFormatInterval(dtmStart As Date, datend As Date, _
Optional strFormat As String = "H:MM:SS") As String
' Return the difference between two times,
' formatted as specified in strFormat.
' From "VBA Developer's Handbook"
' by Ken Getz and Mike Gilbert
' Copyright 1997; Sybex, Inc. All rights reserved.
' Requires:
' GetTimeDelimiter
' In:
' dtmStart:
' starting date for the interval, including a time portion
' datend:
' ending date for the interval, including a time portion
' strFormat (optional):
' format specifier, as shown below. (Default: "H:MM:SS")
' Out:
' Return Value:
' The formatted time difference.
' Comment:
' Due to the way the calculations are performed, the largest
interval
' is 68 years or so.
' Example:
' Using #1/1/97 12:00 PM# and #1/4/97 2:45:45 PM# as the dates,
and one
' of the following format templates,
' dhFormatInterval(#1/1/97 12:00 PM#, #1/4/97 2:45:45 PM#,
"<format>")
' will return (using each of the following format strings):
' D H 3 Days 3 Hours
' D H M 3 Days 2 Hours 46 Minutes
' D H M S 3 Days 2 Hours 45 Minutes 45 Seconds
' D H:MM 3 Days 2:46
' D HH:MM 3 Days 02:46
' D HH:MM:SS 3 Days 02:45:45
' H M 74 Hours 46 Minutes
' H:MM 74:46 (leading 0 on minutes, if necessary)
' H:MM:SS 74:45:45
' M S 4485 Minutes 45 Seconds
' M:SS 4485:45 (leading 0 on seconds, if
necessary)
Dim lngSeconds As Long