Object - ToNullableDateTimeOrDefault

An object extension method that converts this object to a nullable date time or default.

Try it

public static void Main()
{
	object val1 = "06 July 2008 7:32:47 AM";
    object val2 = "17:32:47.003";
	object thisNull = null;
	object thisInvalid = "FizzBuzz";
	
	DateTime defaultVal = "1/1/2000 12:00:00 AM".ToDateTime();
	
	string format = "{0, -30} {1, -15}";
	
	Console.WriteLine(format, "Object", "ToNullableDateTimeOrDefault");
	Console.WriteLine(format, "------", "---------------------------");
	
	// C# Extension Method: Object - ToNullableDateTimeOrDefault
	Console.WriteLine(format, val1, val1.ToNullableDateTimeOrDefault());
	Console.WriteLine(format, val2, val2.ToNullableDateTimeOrDefault());
	Console.WriteLine(format, "null", thisNull.ToNullableDateTimeOrDefault());
	Console.WriteLine(format, thisInvalid, thisInvalid.ToNullableDateTimeOrDefault());
	Console.WriteLine(format, thisInvalid, thisInvalid.ToNullableDateTimeOrDefault(defaultVal));
	Console.WriteLine(format, thisInvalid, thisInvalid.ToNullableDateTimeOrDefault(() => defaultVal));
}

View Source
using System;

public static partial class Extensions
{
    /// <summary>
    ///     An object extension method that converts this object to a nullable date time or default.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <returns>The given data converted to a DateTime?</returns>
    public static DateTime? ToNullableDateTimeOrDefault(this object @this)
    {
        try
        {
            if (@this == null || @this == DBNull.Value)
            {
                return null;
            }

            return Convert.ToDateTime(@this);
        }
        catch (Exception)
        {
            return default(DateTime);
        }
    }

    /// <summary>
    ///     An object extension method that converts this object to a nullable date time or default.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <param name="defaultValue">The default value.</param>
    /// <returns>The given data converted to a DateTime?</returns>
    public static DateTime? ToNullableDateTimeOrDefault(this object @this, DateTime? defaultValue)
    {
        try
        {
            if (@this == null || @this == DBNull.Value)
            {
                return null;
            }

            return Convert.ToDateTime(@this);
        }
        catch (Exception)
        {
            return defaultValue;
        }
    }

    /// <summary>
    ///     An object extension method that converts this object to a nullable date time or default.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <param name="defaultValueFactory">The default value factory.</param>
    /// <returns>The given data converted to a DateTime?</returns>
    public static DateTime? ToNullableDateTimeOrDefault(this object @this, Func<DateTime?> defaultValueFactory)
    {
        try
        {
            if (@this == null || @this == DBNull.Value)
            {
                return null;
            }

            return Convert.ToDateTime(@this);
        }
        catch (Exception)
        {
            return defaultValueFactory();
        }
    }
}