To Date Time - CSharp System

CSharp examples for System:DateTime Convert

Description

To Date Time

Demo Code


using System.Globalization;
using System;//from   ww  w .  ja v  a  2  s.  c o m

public class Main{
        public static DateTime ToDateTime(this string dateAsString, params string [] formats)
        {
            return string.IsNullOrEmpty(dateAsString) ? DateTime.MinValue : DateTime.ParseExact(dateAsString, formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
        }
        public static DateTime ToDateTime(this string dateAsString, string format)
        {
            return string.IsNullOrEmpty(dateAsString) ? DateTime.MinValue : DateTime.ParseExact(dateAsString, format, CultureInfo.InvariantCulture);
        }
        public static DateTime ToDateTime(this string dateAsString)
        {
            return string.IsNullOrEmpty(dateAsString) ? DateTime.MinValue : DateTime.Parse(dateAsString);
        }
}

Related Tutorials