Reputation: 2761
I have a string that is in the format of a date. I want to convert it to a DateTime
object. However I am not certain of the format of the string.
Are there any methods that I can use so that it will work on following inputs?
"12/31/2015", "31/12/2015", "2015-12-31"
Upvotes: 2
Views: 1046
Reputation: 8765
you can use DateTime.Parse
. try this code:
// Define cultures to be used to parse dates.
CultureInfo[] cultures = {CultureInfo.CreateSpecificCulture("en-US"),
CultureInfo.CreateSpecificCulture("fr-FR"),
CultureInfo.CreateSpecificCulture("de-DE")};
// Define string representations of a date to be parsed.
string[] dateStrings = {"01/10/2009 7:34 PM",
"10.01.2009 19:34",
"10-1-2009 19:34" };
// Parse dates using each culture.
foreach (CultureInfo culture in cultures)
{
DateTime dateValue;
Console.WriteLine("Attempted conversions using {0} culture.",
culture.Name);
foreach (string dateString in dateStrings)
{
try {
dateValue = DateTime.Parse(dateString, culture);
Console.WriteLine(" Converted '{0}' to {1}.",
dateString, dateValue.ToString("f", culture));
}
catch (FormatException) {
Console.WriteLine(" Unable to convert '{0}' for culture {1}.",
dateString, culture.Name);
}
}
Console.WriteLine();
Upvotes: 0
Reputation: 98750
DateTime.TryParseExact
method has an overload which takes your formats as a string array.
string s = "";
DateTime dt;
var array = new[] {"MM/dd/yyyy", "dd/MM/yyyy", "yyyy-MM-dd"};
if(DateTime.TryParseExact(s, array, CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
//
}
I used InvariantCulture
as a IFormatProvider
because /
format specifier has a special meaning of replace me with current culture or supplied culture date separator. That means, if you use CurrentCulture
and it doesn't have /
as a DateSeparator
, your parsing will fail even if your string and format exactly matches.
This way is okey for your examples.
But string like 01/02/2015
is a problem because this method can't know that this is 1 February 2015
or 2 January 2015
. In such a case, this method parse your string with first successfully matched format.
However I am not certain of the format of the string.
If you want to full successfully parse for your all examples, you have to know their exact formats.
Upvotes: 4
Reputation: 17957
You can use TryParseExact with a list of expected formats.
string[] formats = { "MM/dd/yyyy", "dd/MM/yyyy", "yyyy-MM-dd" };
DateTime outputDate;
DateTime.TryParseExact(inputText, formats,
CultureInfo.InvariantCulture, DateTimeStyles.None, out outputDate)
Upvotes: 4
Reputation: 10015
you can use list of allowed cultures:
var list = new List<string> {"12/31/2015", "31/12/2015", "2015-12-31"};
var allowedCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
foreach (string s in list)
{
DateTime dt;
foreach (CultureInfo culture in allowedCultures)
{
if (DateTime.TryParse(s, culture, DateTimeStyles.None, out dt))
{
Console.WriteLine("{0} - {1}", culture.DisplayName, dt.ToShortDateString());
break;
}
}
}
Upvotes: 0