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

Ektron Framework API – List Folders

Posted on February 3, 2015October 26, 2015

I found this code on the Ektron Developer Center (see link below) that pertained mainly to menus but assisted me in getting a folder drill-down so I could add it into my DMS content widget. Similar to the DMS content widget make sure you setup the proper permissions to the…

Read More

Write TXT File From textbox.text

Posted on May 13, 2013October 26, 2015

System.IO.File.WriteAllText(usertxt", txtbox.Text); Originally Posted on May 13, 2013Last 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…

Read More

Feodra – Setup Desktop Environment

Posted on October 30, 2017January 18, 2019

In a recent turn of events I’ve been asked to setup a proof of concept (POC) UNIX desktop environment for a work location. Below I will work through some of the process. First I chose to start with Fedora KDE Spin available at https://spins.fedoraproject.org/kde/ as the users will be coming…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • PowerShell - Get Text Encoding Type
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - IIS Remove Site
  • Javascript - Digital Clock with Style

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
  • PowerShell - Get Text Encoding Type
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - IIS Remove Site
  • Javascript - Digital Clock with Style
  • Bleaching Wool using Hydrogen Peroxide
©2025 David Kittell | WordPress Theme by SuperbThemes
David Kittell
  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support