Check if DateTime is valid

S

shapper

Hello,

Is there a way to check if a day, month and year values create a valid
date?

I am using the following:

try {
DateTime start = new DateTime(o.StartYear, o.StartMonth,
o.StartDay);
}
catch {
return new ValidationFailure("StartDate", "Seleccione a data
inicial");
}
return null;

Can I have do this check without using try/catch and not firing an
exception?

Thank You,
Miguel
 
A

Arne Vajhøj

Is there a way to check if a day, month and year values create a valid
date?

I am using the following:

try {
DateTime start = new DateTime(o.StartYear, o.StartMonth,
o.StartDay);
}
catch {
return new ValidationFailure("StartDate", "Seleccione a data
inicial");
}
return null;

Can I have do this check without using try/catch and not firing an
exception?

You could concatenate a string and use DateTime.TryParse.

You could implement the necessary logic yourself.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top