-1

The problem is to convert a random formatted (12/08/12-08-2019/12AUG/120819 etc) into DateTime. Since format is not fixed I'm using TryParse, it does work for normal cases but not for "120819".

string text="270619";

DateTime dt=new DateTime();

Thread.CurrentThread.CurrentCulture=CultureInfo.GetCultureInfo("en-IN");

var result=
DateTime.TryParse(text,CultureInfo.CurrentCulture,DateTimeStyles.None,out dt);

Console.WriteLine(dt);

Expected date should be 27-06-2019 00:00:00

Edit: Currently I'm using formats={"ddMMyy","ddMMyyyy"} and its working, but TryParseExact then fails for the other format like dd-MM-yyyy. Guess I need to write down all the possible formats in there. Or use if else case using both TryParse and TryParseExact.

  • 6
    How can it parse that without knowing what the format is? 120819 could be 12th august 2019 or 8th december 2019 depending on who you ask (it could of course be 1919 but thats a different matter).. The only way is to tell it what the format is, either ddMMyy or MMddyy – Tim Rutter Aug 12 '19 at 12:23
  • 2
    TryParse doesn't guess more than Parse does, it just doesn't throw an exception. Use TryParseExact and pass the appropriate format. See [duplicate](https://stackoverflow.com/questions/341175/datetime-parse-and-making-it-work-with-a-specific-format) (one of many, this question basically gets asked once a day), and [Microsoft Docs: Custom date and time format strings](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings). – CodeCaster Aug 12 '19 at 12:24
  • What date are you actually getting? And what is in `result` if you debug it? – Peter Smith Aug 12 '19 at 12:25
  • 3
    if the format is not fixed, then basically you can't parse it. – Tim Rutter Aug 12 '19 at 12:26
  • @PeterSmith its the default date 01-01-0001 00:00:00 – Shubham Tyagi Aug 12 '19 at 12:32
  • @TimRutter the format would always be like date-month-year but in different formats like ddMMyy or ddMMyyyy or ddMMM etc – Shubham Tyagi Aug 12 '19 at 12:34

1 Answers1

2

Try TryParseExact and provide your format ddMMyy like this:

DateTime.TryParseExact(text, "ddMMyy", System.Globalization.CultureInfo.CurrentCulture,
                                      System.Globalization.DateTimeStyles.None, out dt);
dvo
  • 2,113
  • 1
  • 8
  • 19
  • Currently I'm using formats={"ddMMyy","ddMMyyyy"} and its working, but TryParseExact then fails for the other format like dd-MM-yyyy. – Shubham Tyagi Aug 12 '19 at 12:38
  • You need to have all possibilities in your `formats` array. Treat those as your accepted formats. Otherwise, you could use Regex to format the string yourself before trying to parse it, but that's an extra step. – dvo Aug 12 '19 at 12:45
  • Thanks @dvo. I might just use both TryParse and TryParseExact one after another to avoid writing all the formats bc there are many. – Shubham Tyagi Aug 12 '19 at 12:49
  • Sounds like a very bug prone approach to me. Why is it that you have so many format possibilities? – Tim Rutter Aug 12 '19 at 13:09