如何在 libreoffice 或 VBA 中验证 ISO 格式 (YYYY-MM-DD) 日期验证。如果它不是 YYYY-MM-DD 它应该给我一条错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26146344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:43:44  来源:igfitidea点击:

How to validate a ISO Format (YYYY-MM-DD) date validation in libreoffice or VBA. If its other than YYYY-MM-DD it should give me an error message

excelvbalibreoffice

提问by Rajbir singh

Have to check the date format validation in my code

必须检查我的代码中的日期格式验证

If it's YYYY-MM-DDit is okay to proceed, otherwise exit the loop

如果YYYY-MM-DD可以继续,否则退出循环

something like this

像这样的东西

if date is not in YYYY-MM-DD format  
  Msgbox "please enter a valid format of YYYY-MM-DD  
exit     
else  
   all those calcualtions  
end if

回答by Mr. Mascaro

Use this function:

使用这个功能:

If you're getting the results from a non-user source this should work. If the data is coming from a user I would test it for being any valid date and then convert into the format you need.

如果您从非用户来源获得结果,这应该可行。如果数据来自用户,我会测试它是否为任何有效日期,然后转换为您需要的格式。

Function TestDate(D As String) As String
    Dim X As Date

    On Error GoTo inval

    If Len(D) = 10 Then
        X = CDate(D)
        TestDate = Format(X, "YYYY-MM-DD")
    Else
        TestDate = "Invalid"
    End If

    Exit Function

inval:
    TestDate = "Invalid"
End Function