ConvertToDateTimeNullable

  public static DateTime? ConvertToDateTimeNullable(this object textValue, string format = "")
        {
            DateTime dateTimeValue;

            if (!string.IsNullOrWhiteSpace(format))
            { //if we a have format passed,parse using that format only
                if (DateTime.TryParseExact(textValue == null ? string.Empty : textValue.ToString(), format, System.Globalization.CultureInfo.InvariantCulture,
                    System.Globalization.DateTimeStyles.None, out dateTimeValue))
                {
                    return dateTimeValue;
                }
            }
            else
            {
                if (DateTime.TryParse(textValue == null ? string.Empty : textValue.ToString(), out dateTimeValue))
                    return dateTimeValue;
            }
            return null;
        }
Example:
"".ConvertToDateTimeNullable();

Description

ConvertToDateTimeNullable

Details

Double click on the code to select all.

 

;