In C#, how do I parse a string of format "dd/mm/yyyy"
or format "dd-mm-yyyy"
to datetime?
I get an error that says my string is not in the correct format. I need to be able to parse all the formats not just one of them.
Asked
Active
Viewed 1.4k times
10

Mark Rotteveel
- 100,966
- 191
- 140
- 197

Andris Mudiayi
- 459
- 2
- 6
- 21
-
6"I need to be able to parse all the formats" - given that there exist both `dd/mm/yyyy` and `mm/dd/yyyy` formats, any dates for the 12th or earlier in the month must be ambiguous - it's a problem unsolvable by a computer. – Damien_The_Unbeliever Jul 23 '13 at 14:34
-
Show us the code you've tried. – Jim Mischel Jul 23 '13 at 14:34
-
2What formats are you trying to [parse exactly](http://msdn.microsoft.com/library/332de853.aspx)? – Corak Jul 23 '13 at 14:36
-
possible duplicate of [Parsing DateTime strings](http://stackoverflow.com/questions/1325400/parsing-datetime-strings) – Philip Gullick Jul 23 '13 at 14:38
-
@Damien_The_Unbeliever Actually, even a human can't decide if you intend the 3rd of april or the 4th of March... – Teejay Jul 23 '13 at 14:42
-
@Teejay - yes, that's why "yyyy-MM-dd" should be the one and only format to ever be used by everyone. ever. Well, until the year 9999 at least... – Corak Jul 23 '13 at 14:43
-
1@Corak Since I'm from Italy, I think also **dd/MM/yyyy** is actually good. Definitely, **MM/dd/yyyy** is the most non-sense and confusing one. – Teejay Jul 23 '13 at 14:45
-
@Corak - RFC 2550 deals with that limitation. – Damien_The_Unbeliever Jul 23 '13 at 14:49
-
2@Teejay - Germany here, for us it's dd.MM.yyyy. But that also quickly becomes "wrong" when adding a time. "dd.MM.yyyy HH:mm:ss"? dafuq? Therefore: "yyyy-MM-dd HH:mm:ss" => *logic*! ^_^ – Corak Jul 23 '13 at 14:50
-
You're looking for something like this: http://stackoverflow.com/questions/2326127/datetime-tryparse-all-possible-type-of-dates or http://stackoverflow.com/questions/16211765/parsing-any-valid-datetime-format-to-sql-server-compatible-format ? – nawfal Jan 23 '14 at 05:01
4 Answers
15
You can define any format you like - plus you can get a list of defaults for a given culture.
var ci = new CultureInfo("en-US");
var formats = new[] { "M-d-yyyy", "dd-MM-yyyy", "MM-dd-yyyy", "M.d.yyyy", "dd.MM.yyyy", "MM.dd.yyyy" }
.Union(ci.DateTimeFormat.GetAllDateTimePatterns()).ToArray();
DateTime.ParseExact("07/23/2013", formats, ci, DateTimeStyles.AssumeLocal).Dump();
DateTime.ParseExact("07-23-2013", formats, ci, DateTimeStyles.AssumeLocal).Dump();
DateTime.ParseExact("23-07-2013", formats, ci, DateTimeStyles.AssumeLocal).Dump();
DateTime.ParseExact("23.07.2013", formats, ci, DateTimeStyles.AssumeLocal).Dump();
Output:
7/23/2013 12:00:00 AM
7/23/2013 12:00:00 AM
7/23/2013 12:00:00 AM
7/23/2013 12:00:00 AM

phillip
- 2,618
- 19
- 22
8
mm
means minute, uppercase MM
means month.
Apart from that, you have to use CultureInfo.InvariantCulture
if you want to parse strings with /
as date separator since this is the replacement char for your current culture's date separator:
So this works:
DateTime.ParseExact("23/07/2013", "dd/MM/yyyy", CultureInfo.InvariantCulture);

Tim Schmelter
- 450,073
- 74
- 686
- 939
5
You need to define which date formats are you looking to accept. There's no such thing as all formats. Once you define that you can pass format array to DateTime.ParseExact
Your code can look like this:
string[] formats = new string[2] {"dd/MM/yyyy", "dd-MM-yyyy"};
string date = "23-02-2013";
try
{
DateTime result = DateTime.ParseExact(date, formats, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal);
}
catch (FormatException)
{
// your error handling code here
}

Taras Dzyoba
- 121
- 3
1
You can use DateTime.ParseExact
method with whatever specified format.
var d = DateTime.ParseExact(token, "dd-MM-yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AssumeUniversal);

doogle
- 3,376
- 18
- 23