Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted

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

 

.Net 2.0

			System.Globalization.DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
			myDate = DateTime.TryParseExact(date,"dd-MMM-yy", dtFormat);

.Net 1.0/1.1

	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.

Anybody looking for a graduate programmer (Midlands, England)?
Posted

Well I suppose you could write a method like so...

	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?

Anybody looking for a graduate programmer (Midlands, England)?
Posted

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

Posted

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.

Anybody looking for a graduate programmer (Midlands, England)?
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...