Created
October 29, 2012 19:46
-
-
Save BrunoCaimar/3976084 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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