0

I'm trying to parse a date string in the form dd/mm/yyyy but I keep getting a consistent yet invalid results. What am I doing wrong?

here's the code and it's input

DateTime.ParseExact("23/09/2018","d/m/yyyy",CultureInfo.InvariantCulture) => 1/23/18

DateTime.ParseExact("8/10/2018","d/m/yyyy",CultureInfo.InvariantCulture) => 1/8/18

DateTime.ParseExact("30/04/2018","d/m/yyyy",CultureInfo.InvariantCulture) => 1/30/18

I've tried using CultureInfo.GetCultureInfo("en-UK"), CultureInfo.GetCultureInfo("en-GH") but still same results.

I've tried using "dd/mm/yyyy" still same results.

I've also tried qouting the slashed as described here but still same results.

3 Answers3

4

Try this:

DateTime.ParseExact("23/09/2018","dd/MM/yyyy",CultureInfo.InvariantCulture)

MM/M is used to parse months, mm/m is used for minutes.

kkica
  • 4,034
  • 1
  • 20
  • 40
4

In C# 'M' is used for month and 'm' used for minute, Here you can find all details.

So please try:

   Console.WriteLine(DateTime.ParseExact("8/10/2018","d/M/yyyy",System.Globalization.CultureInfo.InvariantCulture));

for custom implementation you can visit Here.

Sombir Kumar
  • 1,841
  • 1
  • 17
  • 30
2

Small 'm' stands for minutes, try 'M' or 'MM' for months

foxanna
  • 1,570
  • 13
  • 26