验证 vb.net 中的日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24841806/
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-09 17:33:28  来源:igfitidea点击:

Validate the date format in vb.net

vb.netvalidationdatedatetime

提问by Piyush

I have the grid cell value to validate for a correct format as below

我有网格单元格值来验证正确的格式,如下所示

Date value should be in DD-MON-YYYYformat and for this i am using below validation

日期值应为DD-MON-YYYY格式,为此我使用以下验证

Public Function ValidateDateForError(ByVal checkInputValue As String) As Boolean
   Dim returnError As Boolean
    Dim dateVal As DateTime
    If Date.TryParseExact(checkInputValue, "DD-MON-YYYY",
           System.Globalization.CultureInfo.CurrentCulture, 
           DateTimeStyles.None, dateVal) Then
        returnError = True
    Else
        MessageBox.Show("not converted")
    End If
    Return returnError
End Function`

DateTime value should be in DD-MON-YYYY HH:MI:SSformat and for this i am using below validation

日期时间值应该是DD-MON-YYYY HH:MI:SS格式,为此我使用以下验证

Public Function ValidateDateTimeForError(ByVal checkInputValue As String) As Boolean
    Dim returnError As Boolean
    Dim dateVal As DateTime
    If DateTime.TryParseExact(checkInputValue, "DD-MON-YYYY HH:MI:SS",
            System.Globalization.CultureInfo.CurrentCulture, 
            DateTimeStyles.None, dateVal) Then
        returnError = True
    End If
    Return returnError
End Function`

EDate (valid European date) value should be in DD/MM/YYformat and for this i am using below validation

EDate(有效的欧洲日期)​​值应采用DD/MM/YY格式,为此我使用以下验证

 Public Function ValidateEDateForError(ByVal checkInputValue As String) As Boolean
    Dim returnError As Boolean
    Dim dateVal As Date
    If Date.TryParseExact(checkInputValue, "DD/MM/YY",
           System.Globalization.CultureInfo.CurrentCulture, 
           DateTimeStyles.None, dateVal) Then
        returnError = True
    End If
    Return returnError
End Function`

JDate (valid Julian date) value should be in MM/DD/YYformat and for this i am using below validation

JDate(有效的儒略日期)值应采用MM/DD/YY格式,为此我使用以下验证

 Public Function ValidateJDateForError(ByVal checkInputValue As String) As Boolean
    Dim returnError As Boolean
    Dim dateVal As Date
    If Date.TryParseExact(checkInputValue, "MM/DD/YY",
           System.Globalization.CultureInfo.CurrentCulture, 
           DateTimeStyles.None, dateVal) Then
        returnError = True
    End If
    Return returnError
End Function`

but none of above is working. could anyone tell me where i am making mistake?

但以上都不起作用。谁能告诉我我哪里出错了?

Thanks in Advance.

提前致谢。

回答by ??ssa P?ngj?rdenlarp

Using ParseExactmeans that you will be telling it the precise format the string will be in. These are case sensitive to allow things like Hvs hfor 12/24 clock and MMvs mmto distinguish Month from Minutes. So, "DD-MON-YYYY" is invalid, "dd-MM-yyyy" may be what you are after.

使用ParseExact意味着您将告诉它字符串将采用的精确格式。这些区分大小写以允许诸如Hvs h12/24 时钟和MMvsmm区分月份和分钟。所以,“DD-MON-YYYY”是无效的,“dd-MM-yyyy”可能就是你想要的。

But, this means the user would always have to enter 2 digits for the day and month as in "19-07-2014" or "04-07-2014" which they are often not inclined to do, and seems harsh to impose. TryParseExactwill take an array of formats so you can be flexible:

但是,这意味着用户总是必须输入 2 位数字来表示日期和月份,如“19-07-2014”或“04-07-2014”,他们通常不愿意这样做,而且似乎很难强加。 TryParseExact将采用一系列格式,因此您可以灵活:

 Dim strFoo As String = "19-7-2014"        ' d-MM-yyyy
 Dim strBar As String = "19-07-2014"       ' dd-MM-yyyy
 Dim strJuly4 As String = "4-7-2014"       ' d-M-yyyy

 ' several possible format styles
 Dim formats() As String = {"d-MM-yyyy", "dd-MM-yyyy", 
      "dd-M-yyyy", "d-M-yyyy"}

 Dim thisDt As DateTime

 ' this should work with all 3 strings above
 If DateTime.TryParseExact(strFoo, formats,
                           Globalization.CultureInfo.InvariantCulture,
                           DateTimeStyles.None, thisDt) Then
    Console.WriteLine("Success! {0}", thisDt.ToString)

End If

Most of the time there it is silly to force them to enter "04" for a month or day and d/M/yyyywill work with strings with the leading "0" (but not the reverse!). Its is added here mainly to show how to pass an array of format patterns.

大多数情况下,强迫他们在一个月或一天内输入“04”并d/M/yyyy使用以“0”开头的字符串(但不是相反!)是愚蠢的。在这里添加它主要是为了展示如何传递格式模式数组。

DOconsult MSDN for the proper Standard Date and Time Format Strings

DO查阅MSDN的适当标准日期和时间格式字符串



As for Julian Dates, oneform of them is to use the form yyDDDto denote the 2 digit year and day of year for the last 3 digits. I dont know if the convention for this changed after 1/1/2000 when all Julian for this Century would sort below those for the 1990s. Still, here is how it is done:

至于儒略日期,其中一种形式是使用形式yyDDD来表示最后 3 位数字的 2 位年份和年份。我不知道在 2000 年 1 月 1 日之后,当本世纪的所有朱利安都低于 1990 年代的惯例时,这一惯例是否有所改变。不过,这是如何完成的:

Dim jdt As DateTime = #2/11/2010#
Dim jdate As String = xdt.Year.ToString & xdt.DayOfYear.ToString("000")
' ===>  '2010042   or 42nd day of 2010

jdate = (xdt.Year - 2000).ToString("00") & xdt.DayOfYear.ToString("000")
' ===>  '10042   or 42nd day of 2010