设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1712|回复: 2
打印 上一主题 下一主题

[其它] [灌水]怎么把文本日期转化为日期型

[复制链接]
跳转到指定楼层
1#
发表于 2003-11-21 17:38:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
 楼主| 发表于 2003-11-21 17:41:00 | 只看该作者
怎么把数字转化为日期型

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
3#
发表于 2003-11-22 05:25:00 | 只看该作者
很好
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-29 21:29 , Processed in 0.083747 second(s), 26 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表