Check If String Is DateTime in C#

This post is helpful to Check If String Is DateTime in C#

C# has following method to work with DateTime

  • Convert.ToDateTime
  • DateTime.Parse
  • DateTime.TryParse
  • DateTime.TryParseExact

All these methods converts the string representation of a date and time to its DateTime equivalent. The main difference among these methods is how they handle the wrong input. Please Click Here to learn more about these methods.

We used DateTime.TryParseExact method of C# to Check If String Is DateTime.

Date Validator Function:

using System;
using System.Globalization;

namespace TestConsoleApp
{
    public static class CustomDateValidators
    {
        public static bool ValidateStringAsDate(string sDate, out DateTime oDate)
        {
            oDate = DateTime.MinValue;
            if (string.IsNullOrEmpty(sDate)) return false;            

            if (!DateTime.TryParseExact(sDate, "MMddyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out oDate))
            {
                if (!DateTime.TryParseExact(sDate, "yyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out oDate))
                {
                    if (!DateTime.TryParseExact(sDate, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out oDate))
                    {
                        if (!DateTime.TryParseExact(sDate, "MMddyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out oDate))
                        {
                            if (!DateTime.TryParseExact(sDate, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out oDate))
                            {
                                if (!DateTime.TryParseExact(sDate, "MM/dd/yy", CultureInfo.InvariantCulture, DateTimeStyles.None, out oDate))
                                {
                                    if (!DateTime.TryParseExact(sDate, "yyyy/MM/dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out oDate))
                                    {
                                        return false;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return true;
        }
    }
}
 

Call this method from your application where it required.

Example:

namespace TestConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime dt;
            var validDate = CustomDateValidators.ValidateStringAsDate("122944", out dt);
            Console.WriteLine("Converted Date (Formart - MMddyy)     : " + dt + " Valid Date: " + validDate);

            dt = DateTime.MinValue;
            validDate = CustomDateValidators.ValidateStringAsDate("441229", out dt);
            Console.WriteLine("Converted Date (Formart - yyMMdd)     : " + dt + " Valid Date: " + validDate);

            dt = DateTime.MinValue;
            validDate = CustomDateValidators.ValidateStringAsDate("19441229", out dt);
            Console.WriteLine("Converted Date (Formart - yyyyMMdd)   : " + dt + " Valid Date: " + validDate);

            dt = DateTime.MinValue;
            validDate = CustomDateValidators.ValidateStringAsDate("12291944", out dt);
            Console.WriteLine("Converted Date (Formart - MMddyyyy)   : " + dt + " Valid Date: " + validDate);

            dt = DateTime.MinValue;
            validDate = CustomDateValidators.ValidateStringAsDate("12/29/1944", out dt);
            Console.WriteLine("Converted Date (Formart - MM/dd/yyyy) : " + dt + " Valid Date: " + validDate);

            dt = DateTime.MinValue;
            validDate = CustomDateValidators.ValidateStringAsDate("12/29/44", out dt);
            Console.WriteLine("Converted Date (Formart - MM/dd/yy)   : " + dt + " Valid Date: " + validDate);

            dt = DateTime.MinValue;
            validDate = CustomDateValidators.ValidateStringAsDate("1944/12/29", out dt);
            Console.WriteLine("Converted Date (Formart - yyyy/MM/dd) : " + dt + " Valid Date: " + validDate);

            dt = DateTime.MinValue;
            validDate = CustomDateValidators.ValidateStringAsDate("131313", out dt);
            Console.WriteLine("Converted Date (wrong date)           : " + dt + " Valid Date: " + validDate);

            dt = DateTime.MinValue;
            validDate = CustomDateValidators.ValidateStringAsDate("", out dt);
            Console.WriteLine("Converted Date (empty string)         : " + dt + " Valid Date: " + validDate);

            Console.ReadLine();
        }
    }
} 
Output:
Check If String Is DateTime