Monday 23 March 2015

Date format check

Date format check





 public static string ToMyDateTime(DateTime dateTime)
        {
            return dateTime.ToString("dd/MM/yyyy");
        }

        /// <summary>
        /// This method is used to check if the specified expression is a valid date.
        /// This function checks the specified string against MM/DD/YYYY format.
        /// </summary>
        /// <param name="expression">Expression to be checked.</param>
        /// <returns>true if the specified expression is a valid date, false otherwise.</returns>
        public static bool IsValidExectDate(string expression)
        {
            bool bResult = true;

            if (expression == null || expression.Length < 8 || expression.Length > 10)
            {
                bResult = false;
            }
            else
            {
                try
                {
                    string[] expressionArray = expression.Split('/');
                    expression = expressionArray[0].PadLeft(2, '0') + "/" + expressionArray[1].PadLeft(2, '0') + "/" +
                                 expressionArray[2].PadLeft(2, '0');
                    DateTime.ParseExact(expression, "MM/dd/yyyy", CultureInfo.InvariantCulture);
                }
                catch (Exception)
                {
                    bResult = false;
                }
            }

            return bResult;
        }


        /// <summary>
        /// This method is used to check if the specified expression is a valid date.
        /// This function checks the specified string against global date format.
        /// </summary>
        /// <param name="expression">Expression to be checked.</param>
        /// <param name="expression">Global date format.</param>
        /// <returns>true if the specified expression is a valid date, false otherwise.</returns>
        public static bool IsValidExactDate(string expression, string dateFormat)
        {
            bool bResult = true;

            if (expression == null || expression.Length < 8 || expression.Length > 10)
            {
                bResult = false;
            }
            else
            {
                try
                {
                    //expression = expression.Replace('-', '/');
                    string[] expressionArray = expression.Split('/');
                    expression = expressionArray[0].PadLeft(2, '0') + "/" + expressionArray[1].PadLeft(2, '0') + "/" +
                                 expressionArray[2].PadLeft(2, '0');

                    DateTime.ParseExact(expression, dateFormat, CultureInfo.InvariantCulture);
                }
                catch (Exception)
                {
                    bResult = false;
                }
            }

            return bResult;
        }

        /// <summary>
        /// This method is used to check if the specified expression is a valid datetime.
        /// This function checks the specified string against global datetime format.
        /// </summary>
        /// <param name="expression">Expression to be checked.</param>
        public enum DateTimeFormat
        {
            /// <summary>
            /// Use this when you need DateTime to be converted into Date in MMDDYYYY format.
            /// </summary>
            DateMMDDYYYY,

            /// <summary>
            /// Use this when you need DateTime to be converted into Date in MMDDYY format.
            /// </summary>
            DateMMDDYY,

            /// <summary>
            /// Use this when you need DateTime to be converted into Date in DDMMYYYY format.
            /// </summary>
            DateDDMMYYYY,

            /// <summary>
            /// Use this when you need DateTime to be converted into Date in DDMMYY format.
            /// </summary>
            DateDDMMYY,

            /// <summary>
            /// Use this when you need DateTime to be converted into Time in 12 hours clock format.
            /// </summary>
            Time12HoursClock,

            /// <summary>
            /// Use this when you need DateTime to be converted into Time in 24 hours clock format.
            /// </summary>
            Time24HoursClock,

            /// <summary>
            /// Use this when you need DateTime to be converted into Date and Time, with Date in MMDDYYYY format and Time in 12 hours clock format.
            /// </summary>
            DateMMDDYYYYAndTime12HoursClock,

            /// <summary>
            /// Use this when you need DateTime to be converted into Date and Time, with Date in MMDDYY format and Time in 12 hours clock format.
            /// </summary>
            DateMMDDYYAndTime12HoursClock,

            /// <summary>
            /// Use this when you need DateTime to be converted into Date and Time, with Date in DDMMYYYY format and Time in 12 hours clock format.
            /// </summary>
            DateDDMMYYYYAndTime12HoursClock,

            /// <summary>
            /// Use this when you need DateTime to be converted into Date and Time, with Date in DDMMYY format and Time in 12 hours clock format.
            /// </summary>
            DateDDMMYYAndTime12HoursClock,

            /// <summary>
            /// Use this when you need DateTime to be converted into Date and Time, with Date in MMDDYYYY format and Time in 24 hours clock format.
            /// </summary>
            DateMMDDYYYYAndTime24HoursClock,

            /// <summary>
            /// Use this when you need DateTime to be converted into Date and Time, with Date in MMDDYY format and Time in 24 hours clock format.
            /// </summary>
            DateMMDDYYAndTime24HoursClock,

            /// <summary>
        }
    }