Click here to Skip to main content
15,905,682 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi all,

I have two text boxes in which I get the From Date and To date using the calender control in 'dd/MM/yyyy' format.

I need to put a validation that the 'From Date' should not be greater than then 'To Date' but I am constantly getting the error 'String was not recognized as a valid DateTime'.

I am using the following line of code:

VB
Dim mydate1 As Date
Dim mydate2 As Date

mydate1 = DateTime.ParseExact(WDC_From.Text, "MM/dd/yyyy hh:mm", Nothing)
mydate2 = DateTime.ParseExact(WDC_To.Text, "MM/dd/yyyy hh:mm", Nothing)



Does any body know how can I resolve this?

I can not use the Compare Validator due to certain constraints.

Regards,
Gopal
Posted

Dont use ParseExact - that tries to match the string to the exact format (MM/dd/yyyy).
Try using Parse or TryParse.

Obviously, make sure that the text string is a valid Datetime field.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-May-11 3:18am    
Abhinav, I voted 4 this time. What you advise is correct, but you cannot say OP cannot use ParseExact at all. So, I added a bit more accurate answer, please see.
--SA
Abhinav S 23-May-11 8:29am    
Oh I did not mean OP cannot ParseExact at all - if the datetime format is valid it should work.
gopalgupta 23-May-11 3:19am    
both the things do not work.... TryParse returns false and Parse is again giving the same error

The problem is specifically occuring when my date is greater than 12th of the month
Abhinav S 23-May-11 8:31am    
I guess you need to use TryParse with IFormatProvider provided.
Sergey Alexandrovich Kryukov 23-May-11 19:12pm    
How come? Check up parameters and actual string format (OK, show this data. Will work.)
--SA
Use Parse or TryParse as Abhinav advised, but you can also use ParseExact or TryParseExact, only check up the match between parameters and the format of the actual text.

—SA
 
Share this answer
 
hi , try this one

Try
    Dim dateString = "25/11/2006 11:45 PM"
    Dim formats As String() = {"dd/MM/yyyy hh:mm tt", "dd/MM/yy hh:mm tt"}
    Dim dateObject As DateTime = DateTime.ParseExact(dateString, formats, System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.NoCurrentDateDefault)
    MessageBox.Show(dateObject.ToString("dd/MM/yyyy HH:mm"))
Catch ex As FormatException
    MessageBox.Show( "Please enter valid date in dd/mm/yyyy format.")
End Try
 
Share this answer
 
What calendar control are you using that returns a "Text" property? The standard Calendar web control doesn't - it returns a SelectedDate, which is the actual DateTime value you are looking for...
 
Share this answer
 
Use Convert.ToDataTime(WDC_From.Text, "MM/dd/yyyy hh:mm", Nothing)
 
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