验证vb.net中的日期格式 [英] Validate the date format in vb.net

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

问题描述



日期值应在 DD-MON-YYYY

中, code>格式,为此我使用以下验证

 公共函数ValidateDateForError(ByVal checkInputValue As String)As Boolean 
Dim returnError As Boolean
Dim dateVal As DateTime
如果Date.TryParseExact(checkInputValue,DD-MON-YYYY,
System.Globalization.CultureInfo.CurrentCulture,
DateTimeStyles.None,dateVal)然后
returnError = True
Else
MessageBox.Show(not converted)
End If
返回returnError
结束函数

DateTime值应在 DD-MON-YYYY HH:MI: SS 格式,为此我使用以下验证

 公共功能ValidateDateTimeForError(ByVal checkInputValue As String) As Boolean 
Dim returnError As Boolean
D im dateVal As DateTime
如果DateTime.TryParseExact(checkInputValue,DD-MON-YYYY HH:MI:SS,
System.Globalization.CultureInfo.CurrentCulture,
DateTimeStyles.None,dateVal)然后
returnError = True
如果
返回returnError
结束函数

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

 公共函数ValidateEDateForError(ByVal checkInputValue As String)As Boolean 
Dim returnError As Boolean
Dim dateVal As Date
如果Date .TryParseExact(checkInputValue,DD / MM / YY,
System.Globalization.CultureInfo.CurrentCulture,
DateTimeStyles.None,dateVal)然后
returnError = True
End If
返回returnError
结束函数

JDate(有效的朱利安日期)值应为在 MM / D D / YY 格式,为此我使用以下验证

 公共函数ValidateJDateForError(ByVal checkInputValue As String)As Boolean 
Dim returnError As Boolean
Dim dateVal As Date
如果Date.TryParseExact(checkInputValue,MM / DD / YY,
System.Globalization.CultureInfo.CurrentCulture ,
DateTimeStyles.None,dateVal)然后
returnError = True
结束If
返回returnError
结束函数`
/ pre>

但以上都没有工作。任何人都可以告诉我我犯错误的地方吗?



感谢提前。

解决方案>

使用 ParseExact 意味着你会告诉它字符串的精确格式。这些区分大小写,允许像 H vs h 12/24时钟和 MM vs mm 以区分月份与分钟。所以,DD-MON-YYYY是无效的,dd-MM-yyyy可能是你以后的。



但是,这意味着用户总是输入日期和月份的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'dM-yyyy

'几种可能的格式样式
Dim formats()As String = {d-MM-yyyy dd-MM-yyyy,
dd-M-yyyy,dM-yyyy}

Dim thisDt As DateTime

'所有3个字符串都超过
如果DateTime.TryParseExact(strFoo,formats,
Globalization.CultureInfo.InvariantCulture,
DateTimeStyles.None,thisDt)然后
Console.WriteLine(Success! {0},thisDt.ToString)

结束如果

那个愚蠢的时候迫使他们进入04一个月或者一天,而 d / M / yyyy 将使用字符串领先的0(但不是相反!)。它主要是为了显示如何传递格式模式的数组。



DO 请咨询MSDN以获得正确的标准日期和时间格式字符串






对于Julian Dates,他们的一种形式是使用 yyDDD 表示最后3位数字的2位数年份和年份。我不知道这个公约是否在2000年1月1日以后改变,当时这个世纪的所有朱利安都会排在20世纪90年代以下。不过,这是如何做的:

  Dim jdt As DateTime =#2/11/2010#
Dim jdate As String = xdt.Year.ToString& xdt.DayOfYear.ToString(000)
'===> '2010042或2010年第42日

jdate =(xdt.Year - 2000).ToString(00)& xdt.DayOfYear.ToString(000)
'===> '10042或2010年第42天


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

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

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:SS format and for this i am using below validation

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/YY format and for this i am using below validation

 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/YY format and for this i am using below validation

 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.

解决方案

Using ParseExact means that you will be telling it the precise format the string will be in. These are case sensitive to allow things like H vs h for 12/24 clock and MM vs mm to distinguish Month from Minutes. So, "DD-MON-YYYY" is invalid, "dd-MM-yyyy" may be what you are after.

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. TryParseExact will take an array of formats so you can be flexible:

 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/yyyy will 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.

DO consult MSDN for the proper Standard Date and Time Format Strings


As for Julian Dates, one form of them is to use the form yyDDD to 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:

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

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

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