1

How can I parse this DateTime value?

17-09-2018 3:18

I want to parse the date and the time. This is what I've tried so far:

x = DateTime.ParseExact(dateString, "d/M/yyyy hh:MM",CultureInfo.InvariantCulture).ToString();
x = DateTime.ParseExact(dateString, "dd/MM/yyyy hh:MM",CultureInfo.InvariantCulture).ToString();
x = DateTime.ParseExact(dateString, "dd/MM/yyyy hh:MM",CultureInfo.InvariantCulture).ToString();

How can I make this work?

Giles Roberts
  • 6,407
  • 6
  • 48
  • 63
  • 1
    `MM` is months, `mm` is minutes. The casing matters. Double `m` or double `M` means the value always has 2 digits. A single `m` or single `M` means the value can have 1 digit if below `10`. – Igor Nov 09 '18 at 20:51
  • 2
    I'd go with `x = DateTime.ParseExact(dateString, "dd-MM-yyyy h:mm",CultureInfo.InvariantCulture).ToString();` – Marco Nov 09 '18 at 20:52
  • Possible duplicate of [Parse string to DateTime in C#](https://stackoverflow.com/questions/5366285/parse-string-to-datetime-in-c-sharp) – Azrael Nov 09 '18 at 21:00
  • Possible duplicate of [datetime.parse and making it work with a specific format](https://stackoverflow.com/questions/341175/datetime-parse-and-making-it-work-with-a-specific-format) – Ozkan Nov 09 '18 at 21:21

2 Answers2

5
  • MM is months, mm is minutes. The casing matters.
  • Double m or double M means the value always has 2 digits. A single m or single M means the value can have 1 digit if below 10.
  • H means hours in military time (24 hour format), h means 12 hour with possible am/pm. The same rule applies for double digits.
  • The character used between values has to match the input string, don't specify - if you are using / or the other way around. Alternativly use a culture parameter that has the same format specifier as in the input string.
 DateTime x = DateTime.ParseExact("17-09-2018 3:18", "d-MM-yyyy h:m", CultureInfo.InvariantCulture);

See also DateTime.ParseExact and Custom Date and Time Format Strings

Igor
  • 60,821
  • 10
  • 100
  • 175
  • 2
    Technically `/` is the date separator in the format and the actual character it matches is based on the culture being used. – juharr Nov 09 '18 at 21:06
  • @juharr - `ParseExact` it will throw a `FormatException` if the separators in the input and format string don't match. Maybe there is an overload I have not considered? – Igor Nov 09 '18 at 21:11
  • 1
    https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#dateSeparator – juharr Nov 09 '18 at 21:30
  • @juharr - Interesting, thanks for the link. I just can't seem to get it to work with `DateTime.ParseExact` where a different `/` and `-` format specifier is used between the input and the format string. It always throws a `FormatException`. [.net fiddle](https://dotnetfiddle.net/z2fSKG) – Igor Nov 09 '18 at 21:41
  • 1
    Not sure what you mean by the last comment. You can try [.NET Fiddle VEElcC](https://dotnetfiddle.net/VEElcC), modified from yours. When the culture uses `"-"` as its default date separator, the character `/` in the string `format` stands for this string, `"-"`. – Jeppe Stig Nielsen Nov 09 '18 at 22:14
  • My mistake was using a culture that had a / as a separator and doing the reverse @JeppeStigNielsen, thank you for the example code. It is clear to me now. – Igor Nov 09 '18 at 23:30
0

You are almost there. You just need to remember two more things:

  1. mm is for minutes and MM is for month.
  2. It's better to use DateTime.TryParseExact and not let the program throw exception interrupting the flow unless that is intended.

    // input string
    string dateString = "5/01/2009 09:00";
    
    // variable to hold output value
    DateTime dateValue;
    
    // specify all the valid formats here applicable in your case
    string[] formats = { "dd/MM/yyyy hh:mm", "dd/M/yyyy hh:mm", "d/M/yyyy hh:mm", "d/MM/yyyy hh:mm", "dd/MM/yy hh:mm", "dd/M/yy hh:mm", "d/M/yy hh:mm", "d/MM/yy hh:mm"};
    if (DateTime.TryParseExact(dateString, formats, CultureInfo.InvariantCulture, 
                             DateTimeStyles.None, out dateValue))
    Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                       dateValue.Kind);
    
Gaurav
  • 154
  • 1
  • 4