Recently I had to validate dates in a form specifically the date of visit and date of birth
This function will now check both for future date and do more checks for the date of visit, debug/helper information left in to better understand the function.
Function Code:
public bool bCheckDate(string sDate, int type)
{
bool bGoodDate = false;
DateTime temp;
if (DateTime.TryParse(sDate, out temp))
{
// if a valid date then proceed
// result.InnerHtml += "<br/>Valid Date: " + temp.ToString();
DateTime todaydate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); // Today
if (type == 0)
{
bGoodDate = false;
if (temp <= todaydate)
{
// Date is not a future date proceed
bGoodDate = true;
}
else
{
// Date is a future date don't proceed
bGoodDate = false;
}
}
if (type == 1)
{
bGoodDate = false;
DateTime highDate = new DateTime(DateTime.Now.Year, 01, 14); // 1-14-Current Year
DateTime lowDate = new DateTime(DateTime.Now.Year, 01, 01); // 1-1-Current Year
DateTime oldestDate = new DateTime(DateTime.Now.Year - 1, 01, 01); // 1-1-Previous Year
if (temp <= todaydate)
{
// Submission date is not a future date proceed
// result.InnerHtml += "<br/>Date of Visit is not a future date";
if (todaydate >= lowDate && todaydate <= highDate)
{
// If today is within low and high allow for a visit date of the previous year
// result.InnerHtml += "<br/>Today Within " + lowDate.ToString() + " and " + highDate.ToString();
if (temp >= lowDate)
{
// if date of service is newer than or equal to low date no problem proceed
// result.InnerHtml += "<br/>Date of Visit is newer than or equal to " + lowDate.ToString();
bGoodDate = true;
}
else
{
// if date of service is older than low date allow up to 1 year ago.
// result.InnerHtml += "<br/>Date of Visit is older than " + lowDate.ToString();
if (temp >= oldestDate)
{
// if date of service is newer than or equal to oldest date no problem proceed
// result.InnerHtml += "<br/>Date of Visit is newer than or equal to " + oldestDate.ToString();
bGoodDate = true;
}
else
{
// if date of service is older than oldest date
// result.InnerHtml += "<br/>Date of Visit is older than " + oldestDate.ToString();
bGoodDate = false;
}
}
}
else
{
// If today is not within low and high dates visit date of the form must be of current year
// result.InnerHtml += "<br/>Today Is Not Within " + lowDate.ToString() + " and " + highDate.ToString();
if (temp >= lowDate)
{
// if date of service is newer than or equal to low date no problem proceed
//result.InnerHtml += "<br/>Date of Visit is newer than or equal to " + lowDate.ToString();
bGoodDate = true;
}
else
{
// if date of service is older than low date
// result.InnerHtml += "<br/>Date of Visit is older than " + oldestDate.ToString();
bGoodDate = false;
}
}
}
else
{
// Submission date is a future date and shouldn't proceed
// result.InnerHtml += "<br/>Date of Visit is a future date";
bGoodDate = false;
}
}
}
else
{
// if date of visit is not valid send error
// result.InnerHtml += "Date of Visit: Invalid Date";
bGoodDate = false;
}
return bGoodDate;
}
Function Use:
if (bCheckDate(txtMemDOB.Value, 0) == true) // validate a good date for DOB
{
// do something
}
else
{
// don't do something
}
if (bCheckDate(txtMemDOS.Value, 1) == true) // validate a good date for DOS
{
// do something
}
else
{
// don't do something
}
Originally Posted on February 28, 2013
Last Updated on October 26, 2015
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.