Skip to content

Instantly share code, notes, and snippets.

@BrunoCaimar
Created October 29, 2012 19:46
Show Gist options
  • Save BrunoCaimar/3976084 to your computer and use it in GitHub Desktop.
Save BrunoCaimar/3976084 to your computer and use it in GitHub Desktop.
/// <summary>
///
/// </summary>
/// <param name="input">Input date</param>
/// <param name="inputFormat">Input Format:
/// dd - Day format 00
/// MM - Month format 00
/// yyyy - Year format 0000
/// HH - Hour format 00 (24 hours)
/// mm - Minutes format 00
/// ss - Seconds format 00
/// tt - AM/PM designator
///
/// E.G. 2012-10-11 14:10:35 will be parsed with format yyyy-MM-dd HH:mm:ss
/// 30/10/2012 will be parsed with format dd/MM/yyyy
/// 20121024 will be parsed with format yyyyMMdd
/// </param>
/// <returns>DateTime representation</returns>
public static DateTime? SafeToDate(this object input, string inputFormat)
{
DateTime result;
var parse = DateTime.TryParseExact(input.SafeToString(),
inputFormat,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out result);
return parse ? result as DateTime? : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment