Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Check For Valid Date

Posted on February 28, 2013October 26, 2015 By David Kittell

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
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.

Related

Code CSharp

Post navigation

Previous post
Next post

Related Posts

Mac OSX Terminal – Get Active Interface

Posted on June 23, 2016March 6, 2017

This will let you know which network interface you are actively using should you have Wi-Fi and Ethernet connected. route get default | grep interface | awk ‘{print $2}’ Get name of network interface ActiveNetwork=$(route get default | grep interface | awk ‘{print $2}’) ActiveNetworkName=$(networksetup -listallhardwareports | grep -B 1…

Read More

Iterate Form Fields

Posted on February 22, 2013October 26, 2015

string sDiagCode = ""; int nDiagCode = 0; NameValueCollection submittedValuesCollection = Request.Form; foreach (string key in submittedValuesCollection.AllKeys) { if (key.Replace("ctl00$MidContentPlaceHolder$", "").Contains("chk_icd9_")) { if (submittedValuesCollection[key] == "on") { nDiagCode = nDiagCode + 1; sDiagCode += key.Replace("ctl00$MidContentPlaceHolder$chk_icd9_", "") + "|"; } } } foreach (string key in Request.Form.Keys) { if(key.Replace("ctl00$MidContentPlaceHolder$", "").Contains("additionaldiags_")) {…

Read More

Enable/Disable Desktop Saves

Posted on November 9, 2013October 26, 2015

WARNING: Backup the computer you try these scripts on before you run these. These scripts will enable/disable the ability to save anything to the desktop. Windows Registry Editor Version 5.00 [HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionPoliciesExplorer] "NoSaveSettings"=dword:00000001 Windows Registry Editor Version 5.00 [HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionPoliciesExplorer] "NoSaveSettings"=dword:00000000 Originally Posted on November 9, 2013Last Updated on October 26, 2015…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes