Object - ToDateTimeOrDefault
An object extension method that converts this object to a date time or default.
public static void Main() { object val1 = "06 July 2008 7:32:47 AM"; object val2 = "17:32:47.003"; object thisInvalid = "FizzBuzz"; DateTime defaultVal = "1/1/2000 12:00:00 AM".ToDateTime(); string format = "{0, -30} {1, -15}"; Console.WriteLine(format, "Object", "ToDateTimeOrDefault"); Console.WriteLine(format, "------", "-------------------"); // C# Extension Method: Object - ToDateTimeOrDefault Console.WriteLine(format, val1, val1.ToDateTimeOrDefault()); Console.WriteLine(format, val2, val2.ToDateTimeOrDefault()); Console.WriteLine(format, thisInvalid, thisInvalid.ToDateTimeOrDefault()); Console.WriteLine(format, thisInvalid, thisInvalid.ToDateTimeOrDefault(defaultVal)); Console.WriteLine(format, thisInvalid, thisInvalid.ToDateTimeOrDefault(() => defaultVal)); }
View Source
using System; public static partial class Extensions { /// <summary> /// An object extension method that converts this object to a 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 ToDateTimeOrDefault(this object @this) { try { return Convert.ToDateTime(@this); } catch (Exception) { return default(DateTime); } } /// <summary> /// An object extension method that converts this object to a 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 ToDateTimeOrDefault(this object @this, DateTime defaultValue) { try { return Convert.ToDateTime(@this); } catch (Exception) { return defaultValue; } } /// <summary> /// An object extension method that converts this object to a date time or default. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="defaultValue">The default value.</param> /// <param name="useDefaultIfNull">true to use default if null.</param> /// <returns>The given data converted to a DateTime.</returns> public static DateTime ToDateTimeOrDefault(this object @this, DateTime defaultValue, bool useDefaultIfNull) { if (useDefaultIfNull && @this == null) { return defaultValue; } try { return Convert.ToDateTime(@this); } catch (Exception) { return defaultValue; } } /// <summary> /// An object extension method that converts this object to a 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 ToDateTimeOrDefault(this object @this, Func<DateTime> defaultValueFactory) { try { return Convert.ToDateTime(@this); } catch (Exception) { return defaultValueFactory(); } } /// <summary> /// An object extension method that converts this object to a date time or default. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="defaultValueFactory">The default value factory.</param> /// <param name="useDefaultIfNull">true to use default if null.</param> /// <returns>The given data converted to a DateTime.</returns> public static DateTime ToDateTimeOrDefault(this object @this, Func<DateTime> defaultValueFactory, bool useDefaultIfNull) { if (useDefaultIfNull && @this == null) { return defaultValueFactory(); } try { return Convert.ToDateTime(@this); } catch (Exception) { return defaultValueFactory(); } } }