By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,505 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,505 IT Pros & Developers. It's quick & easy.

Checking for valid datetime

P: n/a
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?


*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Mike P" <mi*******@gmail.comwrote:
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?
DateTime.ParseExact.

I think your format string will be "dd/MM/yyyy HH:mm"

Eq.
Jun 27 '08 #2

P: n/a
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
Jun 27 '08 #3

P: n/a
On Jun 3, 9:50 am, "Paul E Collins" <find_my_real_addr...@CL4.org>
wrote:
"Mike P" <mike.p...@gmail.comwrote:
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?

DateTime.ParseExact.
Or DateTime.TryParseExact for checking purposes.
I think your format string will be "dd/MM/yyyy HH:mm"
Agreed.

Jon
Jun 27 '08 #4

P: n/a
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 Developersdex http://www.developersdex.com ***
Jun 27 '08 #5

P: n/a
On Jun 3, 10:57 am, Mike P <mike.p...@gmail.comwrote:
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

Jun 27 '08 #6

P: n/a
On Jun 3, 5:57*am, Mike P <mike.p...@gmail.comwrote:
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
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.