验证日期格式 [英] Validating date format

查看:60
本文介绍了验证日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何验证mm/dd/yyyy格式的日期格式. 我已经尝试过此代码,但是无法正常工作.

Anybody knows how to validate date format in the format of mm/dd/yyyy. I have tried this code but is not working.

在用户表单中:

dateCheck (txtStartDate)

在模块中:

Function dateCheck(dateValue As Date) As Boolean
    If CDate(dateValue) <> "mm/dd/yyyy" Then
        MsgBox "Please use the mm/dd/yyyy  date format!"
        dateCheck = True
    End If
End Function

我在正确的轨道上吗?谢谢!

Am I on the right track? Thanks!

推荐答案

Function dateCheck(dateStrng As String) As Boolean
Dim dateArr as Variant 

If IsDate(dateStrng) Then ' <~~ if it IS a date
   dateArr = Split(dateStrng,"/")
   If UBound(dateArr) = 2 Then '<~~ if it has three substrings separate by two "slashes"
      If CInt(dateArr(0)) < 13 Then  '<~~ if the 1st number is lower or equals the maximum possible month number
         If CInt(dateArr(0)) > 0 Then  '<~~ if the 1st number is higher or equals the mimimum possible month number
            If CInt(dateArr(1)) < 31 Then  '<~~ if the 2nd number is lower or equals the maximum possible day number
               If CInt(dateArr(1)) > 0 Then  '<~~ if the 2nd number is higher or equals the mimimum possible day number
                  If CInt(dateArr(2)) < 10000 Then dateCheck = CInt(dateArr(2)) > 999  '<~~ if the 3rd number is a 4 digit integer "year" number, then check returns True
               End If
            End If
         End If
      End If
   End If
End Function

这篇关于验证日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆