Skip to content

Date Compare Checking

June 27, 2012

On Tuesday last week, I was trying to work out as to why the date compare check validation was not working on server validation in the asdp.net page so I wrote a manual code to check this when you hit the submit button: – 

protected bool dateCompareCheck()
{
System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
provider = new System.Globalization.CultureInfo(“en-US”);

DateTime StartTime;
DateTime EndTime;

if (txtStartTime.Text.Trim() != “”)
{
StartTime = Convert.ToDateTime(txtStartTime.Text.Trim(), provider);
}
else
{
StartTime = Convert.ToDateTime(new DateTime(1970, 1, 1));
}

if (txtEndTime.Text.Trim() != “”)
{
EndTime = Convert.ToDateTime(txtEndTime.Text.Trim(), provider);
}
else
{
EndTime = Convert.ToDateTime(new DateTime(1970, 1, 1));
}

int result = DateTime.Compare(StartTime, EndTime);

if (result <= 0)
{
return true;
}
else
{
return false;
}
}

 

On another page the on server validation page worked so it certainly baffled me as to why one page worked and the other page didn’t.

Here was the code which I made some updates to remove the infamous error of the date range error.

protected void cfvDateCompare_ServerValidate(object source, ServerValidateEventArgs args)
{
System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
provider = new System.Globalization.CultureInfo(“en-US”);

DateTime StartTime;
DateTime EndTime;

if (txtStartTime.Text.Trim() != “”)
{
StartTime = Convert.ToDateTime(txtStartTime.Text.Trim(), provider);
}
else
{
StartTime = Convert.ToDateTime(DateTime.MaxValue);
}

if (txtEndTime.Text.Trim() != “”)
{
EndTime = Convert.ToDateTime(txtEndTime.Text.Trim(), provider);
}
else
{
EndTime = Convert.ToDateTime(DateTime.MinValue);
}

int result = DateTime.Compare(StartTime, EndTime);

if (result <= 0)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}

protected void cfvDateCompare_ServerValidate(object source, ServerValidateEventArgs args)
{
System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
provider = new System.Globalization.CultureInfo(“en-US”);

DateTime StartTime;
DateTime EndTime;

if (txtStartTime.Text.Trim() != “”)
{
StartTime = Convert.ToDateTime(txtStartTime.Text.Trim(), provider);
}
else
{
StartTime = Convert.ToDateTime(new DateTime(1970, 1, 1));
}

if (txtEndTime.Text.Trim() != “”)
{
EndTime = Convert.ToDateTime(txtEndTime.Text.Trim(), provider);
}
else
{
EndTime = Convert.ToDateTime(new DateTime(1970, 1, 1));
}

int result = DateTime.Compare(StartTime, EndTime);

if (result <= 0)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: