Parse datetime

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way using DateTime.Parse to validate a date in the following format?

mm/dd/yyyy

Thanks
 
dwight said:
Is there a way using DateTime.Parse to validate a date in the
following format?

mm/dd/yyyy

Sure - give the format to DateTime.Parse or DateTime.ParseExact. Note
that in custom format strings, "m" is for minute - "M" is for month.
 
If it is valid it will not throw an exception, otherwise it will. Example:

try

{

DateTime a = DateTime.Parse("01/10/200a");

MessageBox.Show("Date is good!");

}

catch (Exception ex)

{

MessageBox.Show("Date is not in proper format.");

}

This will throw an exception and display a message box. Other way is to
use a regular expression.
 
try
{
DateTime a = DateTime.Parse("01/10/200a");
MessageBox.Show("Date is good!");
}
catch (Exception ex)
{
MessageBox.Show("Date is not in proper format.");
}

just a note: a specific Exception should be caught always when possible
As commonly said "Imagine an OutOfMemoryException would be thrown"...
 
Back
Top