标题: [灌水]怎么把文本日期转化为日期型 [打印本页] 作者: 没牙兔兔 时间: 2003-11-21 17:38 标题: [灌水]怎么把文本日期转化为日期型 Function String2Date(strDate As String, strFmt As String) As Date
'
' Converts strings to dates depending on strFmt
' See Help on Format for what the various strings mean
' Comments use 27 May, 1993 as example date to show input text
'
If VarType(strDate) <> 8 Then
String2Date = Null
Exit Function
End If
Select Case strFmt
Case "MMDDYY", "MMDDYYYY" '052793 05271993
String2Date = CVDate(Left(strDate, 2) & "/" & Mid(strDate, 3, 2) & "/" & Mid(strDate, 5))
Case "DDMMYY", "DDMMYYYY" '270593 27051993
String2Date = CVDate(Mid(strDate, 3, 2) & "/" & Left(strDate, 2) & "/" & Mid(strDate, 5))
Case "YYMMDD" '930527
String2Date = CVDate(Mid(strDate, 3, 2) & "/" & Right(strDate, 2) & "/" & Left(strDate, 2))
Case "YYYYMMDD" '19930527
String2Date = CVDate(Mid(strDate, 5, 2) & "/" & Right(strDate, 2) & "/" & Left(strDate, 4))
Case "MM/DD/YY", "MM/DD/YYYY", "M/D/Y", "M/D/YY", "M/D/YYYY", "DD-MMM-YY", "DD-MMM-YYYY"
String2Date = CVDate(strDate)
Case "DD/MM/YY", "DD/MM/YYYY" '27/05/93 27/05/1993
String2Date = CVDate(Mid(strDate, 4, 3) & Left(strDate, 3) & Mid(strDate, 7))
Case "YY/MM/DD" '93/05/27
String2Date = CVDate(Mid(strDate, 4, 3) & Right(strDate, 2) & "/" & Left(strDate, 2))
Case "YYYY/MM/DD" '1993/05/27
String2Date = CVDate(Mid(strDate, 6, 3) & Right(strDate, 2) & "/" & Left(strDate, 4))
Case Else
String2Date = Null
End Select
End Function作者: 没牙兔兔 时间: 2003-11-21 17:41
怎么把数字转化为日期型
Function Num2Date(ByVal intN As Double, strFmt As String) as Date
'
' Converts numbers to dates depending on strFmt
' See Help on Format for what the various strings mean
' Comments use 27 May, 1993 as example date to show input text
'
If Not IsNumeric(intN) Then
Num2Date = Null
Exit Function
End If
intN = CLng(Int(intN))
Select Case strFmt
Case "MMDDYY" '052793
Num2Date = CVDate(intN \ 10000 & "/" & intN \ 100 Mod 100 & "/" & intN Mod 100)
Case "MMDDYYYY" '05271993
Num2Date = CVDate(intN \ 1000000 & "/" & intN \ 10000 Mod 100 & "/" & intN Mod 10000)
Case "DDMMYY" '270593
Num2Date = CVDate(intN \ 100 Mod 100 & "/" & intN \ 10000 & "/" & intN Mod 100)
Case "DDMMYYYY" '27051993
Num2Date = CVDate(intN \ 10000 Mod 100 & "/" & intN \ 1000000 & "/" & intN Mod 10000)
Case "YYMMDD", "YYYYMMDD" '930527 19930527
Num2Date = CVDate(intN \ 100 Mod 100 & "/" & intN Mod 100 & "/" & intN \ 10000)
Case Else
Num2Date = Null
End Select
End Function作者: yodong 时间: 2003-11-22 05:25
很好