Convert To Date - CSharp System

CSharp examples for System:DateTime Convert

Description

Convert To Date

Demo Code


using System.Globalization;
using System.Web;
using System.Linq;
using System.Collections.Generic;
using System;//from   ww  w. j a  v  a  2s . co m

public class Main{
        public static DateTime? ConvertToDate(string inputDateTime, string format = null)
    {
        DateTime outputDateTime;
        DateTime? result = null;
        var parseFormat = format == null ? "dd-MM-yyyy" : format;
        var allFormats = Datetime_Formats(parseFormat);
        foreach (var x in allFormats)
        {
            if (DateTime.TryParseExact(inputDateTime, x, CultureInfo.InvariantCulture, DateTimeStyles.None, out outputDateTime))
            {
                result = outputDateTime;
            }
            if (result != null)
            {
                break;
            }
        }
        return result;
    }
}

Related Tutorials