DateTime and CultureInfo

前端 未结 3 1633
忘了有多久
忘了有多久 2020-12-01 11:02

I have this in my code:

var date1 = DateTime.ParseExact(date, \"dd.MM.yyyy HH:mm:ss\", System.Globalization.CultureInfo.InvariantCulture);

相关标签:
3条回答
  • 2020-12-01 11:12

    Use CultureInfo class to change your culture info.

    var dutchCultureInfo = CultureInfo.CreateSpecificCulture("nl-NL");
    var date1 = DateTime.ParseExact(date, "dd.MM.yyyy HH:mm:ss", dutchCultureInfo);
    
    0 讨论(0)
  • 2020-12-01 11:18

    You may try the following:

    System.Globalization.CultureInfo cultureinfo =
            new System.Globalization.CultureInfo("nl-NL");
    DateTime dt = DateTime.Parse(date, cultureinfo);
    
    0 讨论(0)
  • 2020-12-01 11:33

    InvariantCulture is similar to en-US, so i would use the correct CultureInfo instead:

    var dutchCulture = CultureInfo.CreateSpecificCulture("nl-NL");
    var date1 = DateTime.ParseExact(date, "dd.MM.yyyy HH:mm:ss", dutchCulture);
    

    Demo

    And what about when the culture is en-us? Will I have to code for every single language there is out there?

    If you want to know how to display the date in another culture like "en-us", you can use date1.ToString(CultureInfo.CreateSpecificCulture("en-US")).

    0 讨论(0)
提交回复
热议问题