Dates in C# 2005 Express - Conversion, Validation and Comparison

jcrcarmo

Active member
Joined
Dec 14, 2005
Messages
32
Hi everybody,

Heres a few lines of code for a task that nearly drove me insane: convert, validate and compare dates from two different unbound MaskedTextBoxes. I hope it helps you guys out there... Special thanks to Cags and Marble Eater!

By for now,

JC :)


//------------------------------------------------------------------------------------------------------------------

class DateTimeUtils
{

public static bool IsDate(string strDate)
{
DateTime dtDate;
bool bValid = true;
try
{
dtDate = DateTime.Parse(strDate);
}
catch (FormatException)
{
MessageBox.Show("You have entered an invalid date. ", "Atention", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
bValid = false;
}
return bValid;
}

}


//------------------------------------------------------------------------------------------------------------------

private void DateIni_KeyUp(object sender, KeyEventArgs e)
{
if (this.DateIni.MaskCompleted)
{
string myDI = DateIni.Text;

if (DateTimeUtils.IsDate(myDI))
{
DateFin.Focus();
}
else
{
DateIni.Text = "";
DateIni.Focus();
}
}
}

//------------------------------------------------------------------------------------------------------------------

private void DateFin_KeyUp(object sender, KeyEventArgs e)
{
if (this.DateFin.MaskCompleted)
{
string myDI = DateIni.Text;
string myDF = DateFin.Text;

if (DateTimeUtils.IsDate(myDF))
{
DateTime DI = DateTime.Parse(myDI);
DateTime DF = DateTime.Parse(myDF);

if (DateTime.Compare(DI, DF) < 0)
{
EnableButtons();
}
else
{
MessageBox.Show("The final date value has to be bigger than the initial date value. ", "Atention", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
DisableButtons();
DateFin.Text = "";
DateFin.Focus();
}
}
}
}


//------------------------------------------------------------------------------------------------------------------
 
I pointed this out in your other post as an edit, but Ill put it here too. Code like this is a bad thing...
C#:
		public bool IsDate(object inValue)
		{
			bool bValid;
 
			try 
			{
				DateTime myDT = DateTime.Parse(inValue);
				bValid = true;
			}
			catch (FormatException e) 
			{
				bValid = false;
			}
 
			return bValid;
		}
The reason is that it uses exception handling as a means of validation, which is not what exceptions are meant for. If exceptions can be avoided in a perfectly logical manner they should be. This means that we should not assume dates are formatted correctly, which the DateTime.Parse function does (unless we have a good reason to make such an assumption), but rather use a function that does not make such an assumption: DateTime.TryParse. This way, we can save the use of exceptions for what they are meant for: catastophic, exceptional, unexpected situations. If your code expects an exception, you did something wrong.
 
Hi MarbleEater,

I think understand what you mean, but the code I posted above is a tweaked version of your code and it was the only way I could make it work in my application. Everything else I tried failed... Lets suppose the user enters an incorrect date value by mistake, like for example 30/02/2005 (Feb 30). The TryParse method would make the app halt. How would you rewrite that code? Thanks in advance for your help.

Bye for now,

JC :)
 
Last edited by a moderator:
Using the method marble_eater suggests you dont need an IsDate function at all you would just do the following...
C#:
bool bValid;
DateTime myDate;
string sDate;

bValid = DateTime.TryParse(sDate, out myDate);

if(bValid)
{
   MessageBox.Show("Valid");
   // any other code...
}
else
{
   MessageBox.Show("InValid");
   // any other code...
}

On a side note, I believe this is a 2.0 feature only as my VisualStudio does not support the TryParse() method and as far as I know there isnt a better way of doing it in 1.1 than the one I suggest. I realise the exception would be expensive but am un-aware of any way round the problem.
 
According to MSDN, TryParse is only available for the Double data type in versions 1.0 and 1.1 of the .Net framework. It is available for Int16/32/64, UInt8/16/32/64, byte, single (float), double, DateTime, etc. in version 2.0.
 
Back
Top