-1

I have been trying to solve this problem: in the following code, the local variable 'format4' has a month that is a word and I cannot print it with the ParseExact method. I know that I can use the corresponding integer but cannot figure out how.

using System;

namespace exercise_2
{
     class Program
     {

        static void Main()
        {
            string format1 = "16/03/2020";
            string format2 = "16-03-20";
            string format3 = "03/16/2020"; // US format , month before date
            string format4 = "March 16, 2020";
            DateTime dt_1 = DateTime.ParseExact(format1, "dd/MM/yyyy", null);
            Console.WriteLine(dt_1);
            DateTime dt_2 = DateTime.ParseExact(format2, "dd-MM-yy", null);
            Console.WriteLine(dt_2);
            DateTime dt_3 = DateTime.ParseExact(format3, "MM/dd/yyyy", null);
            Console.WriteLine(dt_3);
            //easier to use the Parse method
            //DateTime dt_4 = DateTime.Parse(format4);
            //Console.WriteLine(dt_4);
            DateTime dt_4 = DateTime.ParseExact(format4, "[MM]" + "[03] dd, yyyy", null);
            Console.WriteLine(dt_4);
        }
    }
}
Eric MSFT
  • 3,246
  • 1
  • 18
  • 28
Enrico
  • 25
  • 4
  • I believe that the only way that you can handle this problem is a method that with switch case return the value that you want .If you need help in writing such a methode I am at your service – sajadre Mar 23 '20 at 18:14
  • Suggested reading: **[Custom date and time format strings](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings)** – Ňɏssa Pøngjǣrdenlarp Mar 23 '20 at 19:33

1 Answers1

3

The format is "MMMM dd, yyyy"

Test code:

Console.WriteLine(DateTime.ParseExact("March 16, 2020", "MMMM dd, yyyy", null));

See also Custom date and time format strings

If the above single line of code does not work then your thread's current culture is not English. To fix this pass the correct culture to the method as the format provider.

Console.WriteLine(DateTime.ParseExact("March 16, 2020", "MMMM dd, yyyy", System.Globalization.CultureInfo.GetCultureInfo("en-US")));
Igor
  • 60,821
  • 10
  • 100
  • 175
  • I tried it that way but it does not work, Visual Studio runs into an exception :( – Enrico Mar 23 '20 at 18:13
  • 1
    @Enrico - If my code above does not work then the only other issue could be that your language on the current thread is not English. – Igor Mar 23 '20 at 18:15
  • `CultureInfo.InvariantCulture` is enough. `null` will fail with non-english languages. – Jimi Mar 23 '20 at 18:43
  • 1
    @Jimi - `CultureInfo.InvariantCulture` is not always the correct answer. See also https://stackoverflow.com/a/9760339/1260204. `"March 16"` is English so it is best to also use an English culture as the format provider like a `CultureInfo` instance of `en-US` or `en-GB` – Igor Mar 23 '20 at 18:46
  • Not always. But here it is. You know the format you're converting from (not to). So, no matter what the local language settings, that string will be parsed correctly. It will of course fail if `IFormatProvider` is null, so `Thread.CurrentThread.CurrentCulture` is used instead. – Jimi Mar 23 '20 at 18:56