IsDate function trouble

ashutosh9910

Freshman
Joined
May 13, 2005
Messages
44
Hi all,

I am facing a strange problem using IsDate function.

Trouble is that IsDate returns true for every valid format of date.

Currently on my website, everywhere for the sake of consistensy, we are using dd-MMM-yy format to display dates.

Now if I put date as 30-Feb-07 which acc to above format is invalid, but
IsDate(30-Feb-07) returns True as it take it as 07-Feb-30 and returns valid.

Now for the sake of consistency, I can not change the format I am using.

Is there any workaround to it.



Thanks in advance.
 
Depending on which version of the framework you are using, you can use either DateTime.TryParseExact() or DateTime.ParseExact().

.Net 2.0
C#:
				System.Globalization.DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
				myDate = DateTime.TryParseExact(date,"dd-MMM-yy", dtFormat);
.Net 1.0/1.1
C#:
		private bool IsCustomDate(string date)
		{
			DateTime myDate = new DateTime();
			bool success = true;

			try
			{
				System.Globalization.DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
				myDate = DateTime.ParseExact(date,"dd-MMM-yy", dtFormat);
			}
			catch (Exception ex)
			{
				success = false;
			}

			return success;
		}

I figure you perhaps wanted VB, the conversion pretty easy, but if you get stuck just say and i'll convert it.
 
Thanks a lot Cag,

That piece of code just works gr8.

The conversion was never an issue. Got it done in just 5 min time.

Working gr8


Thanks again
Ashutosh
 
BTW -

Just for the sake of knowledge,

can I have such a function where in it would take up all the valid date formats and still maintain such strictness of data correction.



Thanks
Ashutosh
 
Well I suppose you could write a method like so...
C#:
		private bool IsCustomDate(string date, string format)
		{
			DateTime myDate = new DateTime();
			bool success = true;
 
			try
			{
				System.Globalization.DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
				myDate = DateTime.ParseExact(date, format, dtFormat);
			}
			catch (Exception ex)
			{
				success = false;
			}
 
			return success;
		}
This would allow you to check any specified format. Is that what you meant?
 
Yes i did the same but the trouble with that is if a user enters say in 21/12/2006, before his entering the date i dont have any idea what format is he going to put.

So if somehow i could get the format of the date entered by the user and pass that to the above function, I am on.

But how can I find the format entered by the user.

Trouble is what format would you take if a user enters
04/05/06
is it 4-May-06
or 5-Apr-06
or anything else.



Thanks
Ashutosh
 
Without creating a complete custom parser I can envisage this being difficult. The problem is the one you highlighted with your first question, the date is a valid date, however it is not the date you intended. It may be possible to minimise this problem using the CultureInfo class, as for example Americans and the British tend to write dates in a differen't order.

Alternatively, if you wanted to accept a few of the different methods only, you could store the acceptable string formats in a string array and loop through them.

The best solution is to make the user of your application input a date in a format specified by you the developer.
 
Yes indeed the best solution as I see to it is to restrict user to put the date in one particular format and not any other and then check the same using the first function.
Applying CultureInfo seems a bit inefficient as in case of a website it won't be a matter of british or american users so... better of be safe and secure.


Thanks any ways.
Ashutosh
 
Back
Top