Checking for valid datetime

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?
 
M

Marc Gravell

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
 
M

Mike P

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);
 
J

Jon Skeet [C# MVP]

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
 
I

Ignacio Machin ( .NET/ C# MVP )

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
 

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

Similar Threads


Top