c# and Date Culture Problems

50

Solution 1

Do like this:

    System.Globalization.CultureInfo cultureinfo = 
        new System.Globalization.CultureInfo("en-gb");
    DateTime dt = DateTime.Parse("13/12/2009", cultureinfo);

Solution 2

You can use DateTime.ParseExact to specify an expected format.

Solution 3

Another option is to specify the culture you wish to use in the web.config file:

<system.web>
    ...
    <globalization 
        culture="da-DK" 
        uiCulture="da-DK" requestEncoding="utf-8" responseEncoding="utf-8"
    />
</system.web>

Solution 4

Yes, ParseExact will do that as mentioned by Matt.

The code would be something like:

dt  = DateTime.ParseExact(sdate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Share:
50
Asddfg
Author by

Asddfg

Updated on June 12, 2022

Comments

  • Asddfg
    Asddfg almost 2 years

    I want to be able to match a substring in a string, but I want my search to be robust to some predefined characters inserted in the original string. To give an example:

    string = "This is a text containing several sentences. This is a first test string\n\n. This test string should also be matched\t."
    substring = "This is a first test string. This test string should also be matched."
    

    I want to return the index of the substring in the original string (typically re.search(substring, string, re.IGNORECASE).spans())

    How can I ignore those meta characters (\n, \t) when searching?

  • MethodMan
    MethodMan about 12 years
    What about it you put "12/12/9999" in theory that's a valid date but from a human standpoint it's not valid so therefore the code you have placed there will never fail for a date where the year is 2012 currently ..we've never reached DateTime.MaxValue of 9999