1

I am trying to parse the following date : 7.92016 which represents September 7th, 2016, using .NET standard functions (DateTime.Parse/TryParse).

So I did the following (with no success as it raises an Invalid Format exception) :

DateTime date;
DateTime.TryParseExact("7.92016", "d.Myyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date);

What bothers me is that is it totally possible to determine where the month ends as the year is specified with "yyyy". So the standard library should be able to manage this.

Of course I could split the string manually for example, but is there a cleaner solution to manage this situation ?

Thanks a lot for your help !

Rimcanfly
  • 53
  • 6
  • If parsing d.myyyy is ok why not m.dyyyy or mdyyyy or yyyydm or yyyy.d.m or yyyy.m.d and so on.... There is no logic of writing the date this way – Dr.Haimovitz Oct 06 '16 at 10:31
  • Anton's answer is quite right. Since `M` _might_ be 2 digit, it maps itself with `92`, not just `9`. Check my answers in the past: [How to parse a string to DateTime with “yyyyMMdd Hmm” format?](http://stackoverflow.com/a/34197474/447156) or [How to convert datetime string in format MMMdyyyyhhmmtt to datetime object?](http://stackoverflow.com/a/35256012/447156) or [DateTime conversion from string C#](http://stackoverflow.com/a/26778076/447156) – Soner Gönül Oct 06 '16 at 12:23

2 Answers2

6

Since M accepts values from 1 to 12 (one or two digits) and it is "greedy" (so that it tries to accept as much digits as possible), the 92 part is what trips TryParseExact off.

Swapping the month and the year, for example, works just fine:

var sept7of2016 = 
    DateTime.ParseExact("7.20169", "d.yyyyM", 
        CultureInfo.InvariantCulture, 
        DateTimeStyles.None);

In other words, your task cannot be done using standard DateTime methods.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
  • You are quite right. In that kind of situations, .NET Team suggest to manipulating _the_ string. Either you insert a leading zero (`0`) for month part, or insert a date separator.You can check these: [How to parse a string to DateTime with “yyyyMMdd Hmm” format?](http://stackoverflow.com/a/34197474/447156) or [How to convert datetime string in format MMMdyyyyhhmmtt to datetime object?](http://stackoverflow.com/a/35256012/447156) or [DateTime conversion from string C#](http://stackoverflow.com/a/26778076/447156) – Soner Gönül Oct 06 '16 at 12:27
  • Thanks, this anwer makes perfect sense to me. I just wondered whether .NET team may have included some "smart" treatments for these situations where determining the date is possible but may ask to involve a bit more of "intelligence". – Rimcanfly Oct 07 '16 at 14:39
1

Not sure of there is another option than to split it manually. if you want it cleaner you can write a function to convert it:

public static DateTime convertDate(string date)
    {
        int pos = date.IndexOf('.');
        return Convert.ToDateTime(date.Substring(0, pos) + "/" +
            date.Substring(pos + 1, (date.Length - 5 - pos)) + "/" +
            date.Substring(date.Length - 4));
    }
Ruan Kruger
  • 33
  • 1
  • 7