473,320 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

validating dates from string input

In C# what is the best way to validate that a particular string
entered by a user will actually convert to a date w/o using a try
catch block or writing code to explicitly parse the string?

Here is what I know I can use, but there are potentially thousands of
dates to validate, and I know this code is inefficient if there are
lots of dates that won't convert...

private DateTime ValidateDate(string date)
{
DateTime convertedDate = DateTime.MinValue;
try
{
convertedDate = DateTime.Parse(date);
}
catch
{
// throw exception for invalid date
}
return convertedDate;
}

Thanks,
John
Nov 16 '05 #1
4 2533
John,

You can use the code you have, or, if you have access to the .NET 2.0
beta, you can use the static TryParse or TryParseExact method on the
DateTime structure.

Another option would be to call the static IsDate method on the
Interaction class in the Microsoft.VisualBasic namespace. It is a little
more lenient than the TryParse and the TryParseExact methods.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Livermore" <jo************@nospm-inginix.com> wrote in message
news:7u********************************@4ax.com...
In C# what is the best way to validate that a particular string
entered by a user will actually convert to a date w/o using a try
catch block or writing code to explicitly parse the string?

Here is what I know I can use, but there are potentially thousands of
dates to validate, and I know this code is inefficient if there are
lots of dates that won't convert...

private DateTime ValidateDate(string date)
{
DateTime convertedDate = DateTime.MinValue;
try
{
convertedDate = DateTime.Parse(date);
}
catch
{
// throw exception for invalid date
}
return convertedDate;
}

Thanks,
John

Nov 16 '05 #2
John Livermore wrote:

In C# what is the best way to validate that a particular string
entered by a user will actually convert to a date w/o using a try
catch block or writing code to explicitly parse the string?
Is there a particular reason that you can't use try/catch?
Here is what I know I can use, but there are potentially thousands of
dates to validate, and I know this code is inefficient if there are
lots of dates that won't convert...
How do you know it is inefficient?

Do you have specific performance requirements that aren't being met? If not,
don't worry about the performance.

The first rule of optimization is: don't do it.


private DateTime ValidateDate(string date)
{
DateTime convertedDate = DateTime.MinValue;
try
{
convertedDate = DateTime.Parse(date);
}
catch
{
// throw exception for invalid date
}
return convertedDate;
}

Nov 16 '05 #3
I have read in many places that try...catch is a means of detecting
errors but it shouldn't be used if you know the error might occur.
Rather you should use other means to detect the condition.
Try...catch is for errors you don't know are going to occur (bugs) to
keep your application behaving nicely.

Your point of don't fix it unless it is broken is well taken, but I
feel this will be an issue for us and should be addressed through some
other means.

We are exploring the use of regular expressions to validate the input
before we attempt to cast it as a date.

On Tue, 07 Sep 2004 12:53:57 -0700, Julie <ju***@nospam.com> wrote:
John Livermore wrote:

In C# what is the best way to validate that a particular string
entered by a user will actually convert to a date w/o using a try
catch block or writing code to explicitly parse the string?


Is there a particular reason that you can't use try/catch?
Here is what I know I can use, but there are potentially thousands of
dates to validate, and

I know this code is inefficient if there are
lots of dates that won't convert...


How do you know it is inefficient?

Do you have specific performance requirements that aren't being met? If not,
don't worry about the performance.

The first rule of optimization is: don't do it.


private DateTime ValidateDate(string date)
{
DateTime convertedDate = DateTime.MinValue;
try
{
convertedDate = DateTime.Parse(date);
}
catch
{
// throw exception for invalid date
}
return convertedDate;
}


Nov 16 '05 #4
John Livermore wrote:

I have read in many places that try...catch is a means of detecting
errors but it shouldn't be used if you know the error might occur.
Rather you should use other means to detect the condition.
Try...catch is for errors you don't know are going to occur (bugs) to
keep your application behaving nicely.
You are right, try/catch is for exceptional circumstances -- in theory. If the
framework provided the necessary methods (such as IsDate()), then try/catch
wouldn't be necessary in this case. Since it doesn't, using try/catch is
perfectly acceptable.
Your point of don't fix it unless it is broken is well taken, but I
feel this will be an issue for us and should be addressed through some
other means.

We are exploring the use of regular expressions to validate the input
before we attempt to cast it as a date.
I was going to suggest regex as well, but didn't. You will probably end up w/
a horrendous piece of code that is difficult to maintain, hard to understand,
and susceptible to a multitude of errors. As I'm sure you are well aware,
date/time formats are locale-dependent.

Further, I wouldn't be surprised in the least if the regex version of what you
come up with is quite a bit slower than the try/catch version. I'd be
interested to hear what you find after you write the regex version and compare
it to the try/catch version...

Good luck.

On Tue, 07 Sep 2004 12:53:57 -0700, Julie <ju***@nospam.com> wrote:
John Livermore wrote:

In C# what is the best way to validate that a particular string
entered by a user will actually convert to a date w/o using a try
catch block or writing code to explicitly parse the string?


Is there a particular reason that you can't use try/catch?
Here is what I know I can use, but there are potentially thousands of
dates to validate, and

I know this code is inefficient if there are
lots of dates that won't convert...


How do you know it is inefficient?

Do you have specific performance requirements that aren't being met? If not,
don't worry about the performance.

The first rule of optimization is: don't do it.


private DateTime ValidateDate(string date)
{
DateTime convertedDate = DateTime.MinValue;
try
{
convertedDate = DateTime.Parse(date);
}
catch
{
// throw exception for invalid date
}
return convertedDate;
}

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: The Plankmeister | last post by:
Hi... What's the best method of validating input characters? I would like to prevent users submitting exotic characters (such as those acquired on Windows Systems by pressing ALT+) and thought...
3
by: Terje | last post by:
Hello I have a function like this function checkquantitiy(quantitiy){ if(quantitiy.value != parseInt(quantitiy.value)) { alert(quantitiy.value+" sorry not integer quantitiy"); } } ...
8
by: netsurfer | last post by:
Hi: Have a question on making the date automatically filled in by what the user enters in by the date at the top. The date entered at the top would most likely be on a Wednesday then I need...
5
by: Steve | last post by:
I am currently trying to validate data in an access database. I need to verify that columns containing date information are in the format ddmmyyyy and columns containg time information are in the...
1
by: panche | last post by:
I'm developing a fairly simple user control that has two textboxes for date/time entry (a from date/time and a to date/time). One of my requirements is that there should be no button that sets...
9
by: chuck | last post by:
I need some help with validating user input. I am writing a C computer program for an intro to C course. Here is the situation. I am creating an application that will do currency conversions. ...
2
by: MadMike42 | last post by:
This is really starting to annoy me, I've got a form, that has some input boxes, a example of the code is here:- <form action="admin_save_stock.asp" method="post" name="MyFormData"> <input...
2
by: Just Me | last post by:
Hi people. I am attempting to determine how best to prevent a users date entry in an update action from causing a format exception. The dates are UK, but the user may try to put in a 'US'...
6
by: Richard | last post by:
I'm validating a date and time string which must be EXACTLY of the format yy-mm-dd hh:mm:ss and extracting the six numeric values using sscanf. I'm using the format string "%2u-%2u-%2u...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.