473,503 Members | 8,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checking for valid datetime

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
6 2121
"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6458
by: Maksim Kasimov | last post by:
there are few of a time periods, for example: 2005-06-08 12:30 -> 2005-06-10 15:30, 2005-06-12 12:30 -> 2005-06-14 15:30 and there is some date and time value: 2005-06-11 12:30 what is the...
3
1466
by: JackO | last post by:
I have a text box on a form “txtExamDate” that I know contains nothing. I am trying to test code that will determine the text box is nothing. I am getting the error because there is no value...
3
2779
by: andrewcw | last post by:
I save date time into my XML files like this: MesObj.theTime=System.DateTime.Now; "5/4/2004 8:50:30 AM" BUT that string is NOT recognizable as a valid dateTime when the data is read back from...
7
2030
by: William Stacey [MVP] | last post by:
Given a day of the month (<=31) how to calculate if that day is valid for all months up to some end date or DateTime.MaxValue? TIA -- William Stacey, MVP
2
2734
by: craigkenisston | last post by:
Hi, I create a one day ticket in my authentication code like this : private void btnLogin_Click(object sender, System.EventArgs e) { string _userId = txtUserId.Text; string _password =...
4
4591
by: SQLScott | last post by:
I have created a VB.Net app that takes data from Visual Foxpro and inserts it into SQL Server. The problem I am running into is that I have come across records in Foxpro where the dates are as...
11
33288
by: Bob Day | last post by:
The IsDate code below should result in False, instead it throws the exception below. Why? How do I check if a string can be converted to a date if this function does not work properly? Bob ...
28
1996
by: romy | last post by:
What's the easiest way to verify the user had entered a valid date ?
1
4667
by: database | last post by:
I AM FACING A PROBLEM WHEN I ACCESS ONE OF THE PAGES. IT USED TO WORK BEFORE BUT AFTER RESTARTING THE APPLICATION SEVER IT GIVES THE FOLLOWING ERROR. - Server Error ...
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5566
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.