Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Validating Date Using C# in ASP.NET

Hi,
This function used to validate date using C#, here i declared DateValidate function and passing date to strDate parameter.

Ex: 21/11/2014  DD/MM/YYYY  

public bool DateValidate(string strDate)
    {
        try
        {
            int day;
            int mon;
            int yr;

            if (strDate.Contains("/") == false)
            {
                return false;
            }
            if (strDate.Length != 10)
            {
                return false;
            }
            string[] Strarray = new string[3];
            char[] splitter = { '/' };
            strArray = strDate.Split(splitter);
            day = Convert.ToInt16(strArray[0]);
            mon = Convert.ToInt16(strArray[1]);
            yr = Convert.ToInt16(strArray[2]);
            if (day > 31)
            {
                return false;
            }
            if (mon > 12)
            {
                return false;
            }
            if (day
            {
                return false;
            }
            if (mon
            {
                return false;
            }
            if (strArray[2].Length
            {
                return false;
            }
            if (yr
            {
                return false;
            }
            if (yr >= 9999)
            {
                return false;
            }
            if (day > DateTime.DaysInMonth(yr, mon))
            {
                return false;
            }
            if (mon == 2)
            {
                if ((DateTime.IsLeapYear(yr) == true) && (day > 29))
                {
                    return false;
                }
                else
                {
                    if ((DateTime.IsLeapYear(yr) == false) && (day > 28))
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        catch
        {
            return false;
        }
    }


This post first appeared on Asp.netSourceCodes, please read the originial post: here

Share the post

Validating Date Using C# in ASP.NET

×

Subscribe to Asp.netsourcecodes

Get updates delivered right to your inbox!

Thank you for your subscription

×