Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

VB
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???
Posted
Comments
[no name] 28-Apr-14 9:01am    
You probably want DateTime.ParseExact. But I would suggest that you are probably doing this wrong. It almost looks like you are going to try and validate a user entered textbox text as a date/time. Use a DateTimePicker or similar control instead.
Amol Jadhao 28-Apr-14 9:02am    
date comming properly but condition is wrong

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.
 
Share this answer
 
v2
can you please check it out below code...

VB
'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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900