Convert To Date Without Null - CSharp System

CSharp examples for System:DateTime Convert

Description

Convert To Date Without Null

Demo Code


using System.Globalization;
using System.Web;
using System.Linq;
using System.Collections.Generic;
using System;/*from w  w  w  . j  a  v  a  2  s . c o m*/

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

Related Tutorials