0

I have two dates as per below and have my local culture is "en-IN"

string enInDateValue = "13-12-2021 12:00:00"; // dd/mm/yyyy
string enUSDateValue = "12-13-2021 12:00:00"; // mm/dd/yyyy

If I run the below code with Invariant Culture it date gets parsed with enUSDate.

DateTime.TryParse(enInDateValue, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out result)
DateTime.TryParse(enUSDateValue, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out result)

To solve the problem below is the code I am using and it is parsing the dates per culture.

public static bool DateTimeTryParse(string date, out DateTime result)
    {
        return (
            DateTime.TryParse(date, CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out result) ||
              DateTime.TryParse(date, CultureInfo.CurrentCulture, DateTimeStyles.NoCurrentDateDefault, out result) ||
              DateTime.TryParse(date, CultureInfo.CurrentUICulture, DateTimeStyles.NoCurrentDateDefault, out result)
        );
    }

If it fails with only Invariant Culture it goes to Current and same for UI Culture

So wanted to ask if that is the right to do? Or is there any other way around?

Anup Shah
  • 167
  • 1
  • 13
  • 1
    No, you need to know the format beforehand. For example, how do you know if `01-02-2021` is 1st February or 2nd January? – DavidG Aug 18 '21 at 09:01
  • 4
    So is 01/02/2021 the first of the second, or the second of the first? You have a bigger problem here – TheGeneral Aug 18 '21 at 09:01
  • @DavidG beat me to it , with the exact same date! – TheGeneral Aug 18 '21 at 09:01
  • @TheGeneral Haha! – DavidG Aug 18 '21 at 09:02
  • 1
    The computer isn't magic - it can't guess the meaning alas. If you have dates, they need to be in a set and well defined format. – mjwills Aug 18 '21 at 09:02
  • 1
    Or you need to store the format (culture) along with the date – Camilo Terevinto Aug 18 '21 at 09:03
  • @DavidG yes that is the question, if api sending me us format and my local format is enIN and storing data to offline db how can I convert the date with Invariant culture? I hope you have negated the question but I was not clear and have doubt hence asked is that wrong? – Anup Shah Aug 18 '21 at 09:39

1 Answers1

0

You need to specify the expected format explicitly. This small sample may be helpful:

        string date1 = "12-13-2021";
        string date2 = "13-12-2021";

        string format1 = "MM-dd-yyyy";
        string format2 = "dd-MM-yyyy";

        DateTime parsedDate1, parsedDate2;

        bool success1 = DateTime.TryParseExact(date1, format1, null, DateTimeStyles.None, out parsedDate1);
        bool success2 = DateTime.TryParseExact(date2, format2, null, DateTimeStyles.None, out parsedDate2);

        if (success1)
            Console.WriteLine($"First date parsed successfully: {parsedDate1.ToString("yyyy,MMM,dd,ddd")}");
        else
            Console.WriteLine("First date failed to parse");

        if (success2)
            Console.WriteLine($"Second date parsed successfully: {parsedDate2.ToString("yyyy,MMM,dd,ddd")}");
        else
            Console.WriteLine("Second date failed to parse");
John
  • 29,788
  • 18
  • 89
  • 130
Gec
  • 428
  • 2
  • 10
  • If you don't have enough rep to comment, then go get some rep. You only need a few points so should only take a few minutes. – DavidG Aug 18 '21 at 09:30