I had posted a question on DateTime to String conversion, I got many satisfying answers for that .. so I thank StackOverflow very much ..
Here is one more problem of String
Here are all the possible formats ..
- MM/dd/yyyy 08/22/2006
- ffffdd, dd MMMM yyyy Tuesday, 22 August 2006
- ffffdd, dd MMMM yyyy HH:mm Tuesday, 22 August 2006 06:30
- ffffdd, dd MMMM yyyy hh:mm tt Tuesday, 22 August 2006 06:30 AM
- ffffdd, dd MMMM yyyy H:mm Tuesday, 22 August 2006 6:30
- ffffdd, dd MMMM yyyy h:mm tt Tuesday, 22 August 2006 6:30 AM
- ffffdd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
- MM/dd/yyyy HH:mm 08/22/2006 06:30
- MM/dd/yyyy hh:mm tt 08/22/2006 06:30 AM
- MM/dd/yyyy H:mm 08/22/2006 6:30
- MM/dd/yyyy HH:mm:ss 08/22/2006 06:30:07
- MMMM dd August 22
- yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2006-08-22T06:30:07.7199222-04:00
- ffffd, dd MMM yyyy HH':'mm':'ss 'GMT' Tue, 22 Aug 2006 06:30:07 GMT
- yyyy'-'MM'-'dd'T'HH':'mm':'ss 2006-08-22T06:30:07
- HH:mm 06:30
- hh:mm tt 06:30 AM
- H:mm 6:30
- h:mm tt 6:30 AM
- HH:mm:ss 06:30:07
- yyyy'-'MM'-'dd HH':'mm':'ss'Z' 2006-08-22 06:30:07Z
- ffffdd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
- yyyy MMMM 2006 August
DateTime dt1 = DateTime.ParseExact("2007/01/01 04:23:12", "yyyy/MM/dd hh:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
DateTime dt = Convert.ToDateTime("2007/01/01 04:23:12", System.Globalization.CultureInfo.CurrentCulture);
System.Globalization.CultureInfo.CurrentCulture format param
You can use the DateTime.ParseExact
overload that takes a list of formats:
private static string[] formats = new string[]
{
"MM/dd/yyyy HH:mm:ss tt",
"MM/dd/yyyy HH:mm:ss",
"M/dd/yyyy H:mm:ss tt",
"M/dd/yyyy H:mm:ss"
};
private static DateTime ParseDate(string input)
{
return DateTime.ParseExact(input, formats, CultureInfo.InvariantCulture, DateTimeStyles.None);
}
This will throw a FormatException
if the passed string does not match any of the given formats. Notice that the formats expecting AM/PM should appear before identical formats without AM/PM ("MM/dd/yyyy HH:mm:ss tt"
comes before "MM/dd/yyyy HH:mm:ss"
).
Update
As Henk points out in the comments, the same functionality is available when using TryParseExact
which removes exception situation. Also, paired with nullable types this can be made a bit cleaner:
private static DateTime? ParseDate(string input)
{
DateTime result;
if (DateTime.TryParseExact(input, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
{
return result;
}
return null;
}
Now it will simply return a null reference if it fails to parse the input.
Take a look at the TryParseExact method. Here's an example with the first case:
DateTime date;
// I changed 02/31/2009 to 01/31/2009 because the first is not a valid date
if (DateTime.TryParseExact("01/31/2009 01:59:59", "MM/dd/yyyy HH:mm:ss", null, DateTimeStyles.None, out date))
{
// string successfully parsed => do something with the date
}
You could then keep a list of different formats and try to parse the string with all of them until you succeed.