vb.net中的简单日期验证 [英] Simple date validation in vb.net

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

问题描述

我想验证日期,它不应该接受少于2014年6月2日。

这里我提到静态日期(01-04-2014),但它会以任何日期格式

i我做错了什么......无法找到



  Dim  dtDate  As  DateTime 
dtDate = Convert.ToDateTime( < span class =code-string> 01-04-2014)
如果(dtDate.Year> = DateTime.Now。年份)然后
如果(dtDate.Day< = 1 (dtDate.Month< = 6 然后
MesgBox( 日期和月份不应小于6月2日
返回 错误
结束 < span class =code-keyword>如果
Else
MesgBox( 年份应该在当年
返回 错误
结束 如果





请帮忙???

解决方案

既然你提到免费格式,那么你应该使用 DateTime.TryParse方法 [ ^ ]验证输入是一个有效的日期。

但是,我建议你使用datepicke r或日历控制将始终返回有效日期,然后无需验证。


您可以在下面的代码中查看它...



 ' 在此处添加您的日期格式,这是您需要的解析 
' 这里我添加了一个,但是如果你得到不同格式的日期那么您需要在格式数组中添加该formte
Dim 格式 As 字符串()= 字符串(){< span class =code-string> dd-MM-yyyy MM-dd-yyyy}

Dim dtDate As DateTime
If DateTime.TryParseExact( 01-04-2014,formats,CultureInfo.InvariantCulture,DateTimeStyles.None,dtDate)然后
dtDate =转换。 ToDateTime( 01-04-2014
如果(dtDate.Year> = DateTime.Now.Year)那么
如果(dtDate.Day< = 1 (dtDate.Month< = < span class =code-digit> 6 )然后
MesgBox( 日期和月份不应少于6月2日
返回 错误
结束 如果
否则
MesgBox( 年份应该在现在年
返回 错误
结束 如果
其他
MesgBox( 请检查您的日期
返回 错误
结束 如果< /跨度>


i wanted to validate date and it should not accept less than 02 june 2014 .
here i have mention static date ("01-04-2014") but it will come any date format
i am doing something wrong...unable to find out

Dim dtDate As DateTime
      dtDate = Convert.ToDateTime("01-04-2014") 
      If (dtDate.Year >= DateTime.Now.Year) Then
          If (dtDate.Day <= 1) And (dtDate.Month <= 6) Then
              MesgBox("Date and month should not less than  2 June ")
              Return False
          End If
      Else
          MesgBox("Year should be in the present Year ")
          Return False
      End If



please help???

解决方案

Since you mention free format, then you should use DateTime.TryParse Method[^] to validate that the input is a valid date.
However, I suggest you use a datepicker or calender control which will always returns a valid date, then no need for validation.


can you please check it out below code...

'add your date format here, which ever you need to parse
       ' here i have added one, but if you are getting date in different format then you need to add that formte in formats array
       Dim formats As String() = New String() {"dd-MM-yyyy", "MM-dd-yyyy"}

       Dim dtDate As DateTime
       If DateTime.TryParseExact("01-04-2014", formats, CultureInfo.InvariantCulture, DateTimeStyles.None, dtDate) Then
           dtDate = Convert.ToDateTime("01-04-2014")
           If (dtDate.Year >= DateTime.Now.Year) Then
               If (dtDate.Day <= 1) And (dtDate.Month <= 6) Then
                   MesgBox("Date and month should not less than  2 June ")
                   Return False
               End If
           Else
               MesgBox("Year should be in the present Year ")
               Return False
           End If
       Else
           MesgBox("please check your date")
           Return False
       End If


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

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