Checking for valid datetime

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

How do I check for a valid datetime, where both the date and the time
must be entered in the format DD/MM/YYYY HH:MM? Using
Convert.ToDateTime would work
even if a time is not added so I can't use this?
 
Another approach; parse it, and then call ToString() specifying the
format you expected. If the "before" and "after" strings aren't the
same, then the input wasn't valid.

Marc
 
In this code example I have found, what classes do I need to reference
to get at DateTimeFormatInfo and DateTimeStyles objects?

DateTime validatedDate;
bool valid = DateTime.TryParseExact(date, date_format,
DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out
validatedDate);
 
In this code example I have found, what classes do I need to reference
to get at DateTimeFormatInfo and DateTimeStyles objects?

DateTime validatedDate;
bool valid = DateTime.TryParseExact(date, date_format,
DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out
validatedDate);

I think you mean the namespace - and System.Globalization is what
you're after.

If you just write the code above though, and put the edit caret in
DateTimeStyles or DateTimeFormatInfo, then hit Ctrl-period, it should
offer to import the right namespace for you. This can be a real time-
saver.

Jon
 
In this code example I have found, what classes do I need to reference
to get at DateTimeFormatInfo and DateTimeStyles objects?

DateTime validatedDate;
bool valid = DateTime.TryParseExact(date, date_format,
DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out
validatedDate);

*** Sent via Developersdexhttp://www.developersdex.com***

Hi,

If you want to know the namespace of a class all you have to do is
type it in MSDN lib, you will get it in the "About" section.
Those classes are in System.Globalization BTW
 
Back
Top