Sample Functions to Parse Numbers and Strings into DatesID: Q185732
|
This article provides two functions that parse strings or numbers into a date based on a format mask.
Microsoft provides programming examples for illustration only, without
warranty either expressed or implied, including, but not limited to, the
implied warranties of merchantability and/or fitness for a particular
purpose. This article assumes that you are familiar with the programming
language being demonstrated and the tools used to create and debug
procedures. Microsoft Support professionals can help explain the functionality
of a particular procedure, but they will not modify these examples to
provide added functionality or construct procedures to meet your specific
needs. If you have limited programming experience, you may want to contact
the Microsoft fee-based consulting line at (800) 936-5200. For more
information about the support options available from Microsoft, please see
the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.asp
Num2Date converts a number to a date.
String2Date converts a string to a date.
MMDDYY MMDDYYYY
DDMMYY DDMMYYYY
YYMMDD YYYYMMDD
MMDDYY MMDDYYYY MM/DD/YY MM/DD/YYYY M/D/Y M/D/YY M/D/YYYY
DDMMYY DDMMYYYY DD/MM/YY DD/MM/YYYY DD-MMM-YY DD-MMM-YYYY
YYMMDD YYYYMMDD YY/MM/DD YYYY/MM/DD
Function Num2Date (ByVal N As Long, ByVal Fmt As String) As Variant
Select Case Fmt
Case "MMDDYY" '052793
Num2Date = CDate(N \ 10000 & "/" & N \ 100 Mod 100 & _
"/" & N Mod 100)
Case "MMDDYYYY" '05271993
Num2Date = CDate(N \ 1000000 & "/" & N \ 10000 Mod 100 & _
"/" & N Mod 10000)
Case "DDMMYY" '270593
Num2Date = CDate(N \ 100 Mod 100 & "/" & N \ 10000 & _
"/" & N Mod 100)
Case "DDMMYYYY" '27051993
Num2Date = CDate(N \ 10000 Mod 100 & "/" & N \ 1000000 & _
"/" & N Mod 10000)
Case "YYMMDD", "YYYYMMDD" '930527 19930527
Num2Date = CDate(N \ 100 Mod 100 & "/" & N Mod 100 & "/" & _
N \ 10000)
Case Else
Num2Date = Null
End Select
End Function
Function String2Date (ByVal S As String, _
ByVal Fmt As String) As Variant
Select Case Fmt
Case "MMDDYY", "MMDDYYYY" '052793 05271993
String2Date = CDate(Left(S, 2) & "/" & Mid(S, 3, 2) & "/" & _
Mid(S, 5))
Case "DDMMYY", "DDMMYYYY" '270593 27051993
String2Date = CDate(Mid(S, 3, 2) & "/" & Left(S, 2) & "/" & _
Mid(S, 5))
Case "YYMMDD" '930527
String2Date = CDate(Mid(S, 3, 2) & "/" & Right(S, 2) & "/" & _
Left(S, 2))
Case "YYYYMMDD" '19930527
String2Date = CDate(Mid(S, 5, 2) & "/" & Right(S, 2) & "/" & _
Left(S, 4))
Case "MM/DD/YY", "MM/DD/YYYY", "M/D/Y", "M/D/YY", "M/D/YYYY", _
"DD-MMM-YY", "DD-MMM-YYYY"
String2Date = CDate(S)
Case "DD/MM/YY", "DD/MM/YYYY" '27/05/93 27/05/1993
String2Date = CDate(Mid(S, 4, 3) & Left(S, 3) & Mid(S, 7))
Case "YY/MM/DD" '93/05/27
String2Date = CDate(Mid(S, 4, 3) & Right(S, 2) & _
"/" & Left(S, 2))
Case "YYYY/MM/DD" '1993/05/27
String2Date = CDate(Mid(S, 6, 3) & Right(S, 2) & _
"/" & Left(S, 4))
Case Else
String2Date = Null
End Select
End Function
?Num2Date(19980203, "YYYYMMDD")
#2/3/98#
?String2Date("020398", "MMDDYY")
#2/3/98#
Dim D1 As Date, D2 As Date
D1 = Num2Date(19980203, "YYYYMMDD")
D2 = String2Date("020398", "MMDDYY")
For additional information, please see the following articles in the Microsoft Knowledge Base:
Q88657 ACC: Functions for Calculating and Displaying Date/Time Values
Q100136 ACC: Two Functions to Calculate Age in Months and YearsFor more information about how Microsoft products are affected by year 2000 (Y2K) issues, please see the following Microsoft World Wide Web site:
http://www.microsoft.com/year2000/
Additional query words: vba y2k kbDSupport kbDSD kbVBA
Keywords : kbdta kb2000 OffVBA
Version : WINDOWS:5.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: July 9, 1999