public static DateTime?TryDateTime(object val, bool useCulture = true) { CultureInfo info = useCulture ? CultureInfo.CurrentCulture : CultureInfo.InvariantCulture; if (val == null) { return(new DateTime?(DateTimeExtension.FromOADate(0.0))); } if (IsNumber(val)) { if (val is DateTime) { return(new DateTime?((DateTime)val)); } if (val is TimeSpan) { TimeSpan span = (TimeSpan)val; return(new DateTime?(DateTimeExtension.FromOADate(span.TotalDays))); } double?nullable = TryDouble(val, useCulture); if (nullable.HasValue) { return(new DateTime?(DateTimeExtension.FromOADate(nullable.Value))); } } else { DateTime time; if ((val is string) && DateTime.TryParse((string)(val as string), (IFormatProvider)info, (DateTimeStyles)DateTimeStyles.None, out time)) { return(new DateTime?(time)); } } return(null); }