Home
Manage Your Code
Snippet: Convert string to "MM/dd/yyyy" format date (C#)
Title: Convert string to "MM/dd/yyyy" format date Language: C#
Description: Returns true if value can be converted to a "MM/dd/yyyy" format date Views: 124
Author: sl prince Date Added: 2/27/2008
Copy Code  
1/// <summary>

2        /// Returns true if value can be converted to a "MM/dd/yyyy" format date

3        /// </summary>

4        /// <param name="anyString"></param>

5        /// <returns>bool</returns>

6        public static bool IsDate(string anyString)
7        {
8            if (anyString == null)
9            { anyString = ""; }
10
11            if (anyString.Length > 0)
12            {
13                string _dateFormat = "MM/dd/yyyy";
14
15                DateTime tempDate;
16                if (DateTime.TryParseExact(anyString, _dateFormat, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out tempDate))
17                {
18                    return true;
19                }
20                else
21                {
22                    return false;
23                }
24            }
25            else { return false; }
26        }