To Date from String by various format - CSharp System

CSharp examples for System:DateTime Format

Description

To Date from String by various format

Demo Code


using System.Globalization;
using System;/*from www. ja v a2  s . c  om*/

public class Main{
        public static DateTime ToDate(this string stringDate)
        {
            if (string.IsNullOrEmpty(stringDate))
                return DateTime.MinValue;
            return DateTime.ParseExact(stringDate,
                new[]
                {
                    "yyyyMMdd", "yyyy-MM-dd", "yyyy/MM/dd", "yyyy.MM.dd",
                    "yyyyMd", "yyyy-M-d", "yyyy/M/d", "yyyy.M.d"
                },
                null, DateTimeStyles.None);
        }
}

Related Tutorials