DateTime.TryParse for any culture
.net, c#, datetime, parsing
Solution
Not really, because everyone has their own date formatting.
What you're basically trying to do is magically deduce the dates from the second half of the above XKCD comic. ;)
That said, the only way you could TRY and do this, is to parse a string with every format you think it might be, and then make a sanity check in every case where a parse was successful. Said sanity check would be difficult however... Does 11-02 mean February 11th or November 2nd? You'd require some sort of context.
Problem
There are lots of thread for this , but I am still blocked at the following: What I have: I am trying to build windows phone App which will pick Date of Birth of an individual Code Behind: ``` string dateString = ""; DateTime dt = DateTime.Now; if (value != null && DateTime.TryParse(value.ToString(), culture, DateTimeStyles.None, out dt)) { if (dt.Equals(DateTime.MinValue)) { //dateString = "mm/dd/yyyy"; return ""; } else return dt.ToShortDateString(); } else return dateString; } ``` what I need: I want it to parse any date format which should be culture Independent. What I tried: 1.I tried using CultureInfo.InvariantCultute, CultureInfo.CurrentCulture 2.Tried using ExactParser as follows: ``` string[] formats = {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", "d/M/yyyy hh:mm:ss tt", "dd/MM/yyyy hh:mm:ss tt", "d/M/yyyy" , "dd/MM/yyyy" , "M/d/yyyy" ,"MM/dd/yyyy", "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", "dd/MM/yyyy hh:mm:ss", "d/M/yyyy h:mm:ss", "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", "d/M/yyyy hh:mm tt", "d/M/yyyy hh tt", "M/d/yyyy h:mm", "M/d/yyyy h:mm", "d/M/yyyy h:mm", "d/M/yyyy h:mm", "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm","dd/MM/yyyy hh:mm", "dd/M/yyyy hh:mm"}; if (value != null && DateTime.TryParseExact(value.ToString(), format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt)) ``` but that is too hardcoded and will not cover several cases. Is there any way to pick DOB in any format? Any help would be Appreciated. Thanks!!