1

I am trying to parse a string using DateTimeOffset.TryParse, it succeeds on Windows but fails in Linux:

DateTimeOffset.TryParse("29.10.2020", out var parsedDate);

I also tried to parse it using

DateTimeOffset.TryParseExact("29.10.2020", "dd.MM.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate) but still the bool returned is false and the default value 0001-01-01T00:00:00.0000000+00:00 is being assgined to parsedDate.

Any way to do this on Linux?

Mihaimyh
  • 1,262
  • 1
  • 13
  • 35
  • 2
    What is your error message? – Casper Dijkstra Nov 02 '20 at 12:07
  • 1
    What is your runtime? Mono or .NET Core? – mu88 Nov 02 '20 at 12:08
  • Wouldn't you be better off using `DateTime.TryParse(...)`? – Martin Nov 02 '20 at 12:08
  • Looks like you're trying to parse a DateTimeOffset when you should be trying to parse a DateTime. Parsing a DateTime from that string should work. A DateTimeOffset also includes a timezone which isn't included in the string you specified. – 230Daniel Nov 02 '20 at 12:08
  • 2
    What do you mean by "fails"? Do you get an error message? if so, what it is? Do you get the wrong value? if so, what it is? Why are you using `DateTimeOffset` if you only have a date? – Zohar Peled Nov 02 '20 at 12:10
  • 1
    [DateTime vs DateTimeOffset](https://stackoverflow.com/questions/4331189/datetime-vs-datetimeoffset) – xdtTransform Nov 02 '20 at 12:19
  • I don't get an error, it is getting parsed as `0001-01-01T00:00:00.0000000+00:00` – Mihaimyh Nov 02 '20 at 12:51
  • @mu88 I am using .NET5 – Mihaimyh Nov 02 '20 at 12:52
  • TryParseExact returns a bool value, if that is false, it means `parsedDate` in your case is default value, so basically the parsing failed, as you observe. – Lasse V. Karlsen Nov 02 '20 at 12:55
  • @ZoharPeled The same method is also parsing string containing time, but there are cases when only dates are passed-in. – Mihaimyh Nov 02 '20 at 12:56
  • @LasseV.Karlsen I know that, the parsing for `29.10.2020` string works on Windows where my development machine is but fails on Linux where the production app is running. – Mihaimyh Nov 02 '20 at 12:58
  • @CasperDijkstra There is no error message, the bool returned by `TryParse` is false and the default value `0001-01-01T00:00:00.0000000+00:00` is assigned to `parsedDate`. – Mihaimyh Nov 02 '20 at 13:01
  • @ZoharPeled The whole point of Try***() methods is to avoid throwing exceptions. I doubt the OP is getting any info about why the method returns false. – Tanveer Badar Nov 02 '20 at 13:11
  • I was asking because you don't seem to have an offset component in your string representation, which leads me to think that you might actually need `DateTime` and not `DateTimeOffset` – Zohar Peled Nov 02 '20 at 13:57
  • @TanveerBadar Yes, I know that. My comment was an attempt to get relevant information from the OP. When I've posted that comment, the question's text was different. Of course I didn't expect `TryParse` to throw an exception, and in fact, any exception thrown from it's equivalent `Parse` method would be just as useless as the `false` returned from `TryParse` - but the point of that comment was to improve the question's quality (and it eventually did - now the question is better) – Zohar Peled Nov 02 '20 at 14:01

2 Answers2

1

This overload of TryParse uses current culture (CultureInfo.CurrentCulture), so culture is different between your dev machine (windows) and your linux machine. For example:

CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en");
DateTimeOffset parsedDate;
bool res = DateTimeOffset.TryParse("29.10.2020", out parsedDate);
Console.WriteLine(res); // false
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ru");
res = DateTimeOffset.TryParse("29.10.2020", out parsedDate);
Console.WriteLine(res); // true

So you need to use overload which accepts IFormatProvider and pass provider there which makes sense for the format in which you store your dates. For example:

var cultureOfMyDates = CultureInfo.GetCultureInfo("ru");
res = DateTimeOffset.TryParse("29.10.2020", cultureOfMyDates, DateTimeStyles.None, out parsedDate);

Or use TryParseExact (your sample with DateTimeOffset.TryParseExact("29.10.2020", "dd.MM.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate) works fine for me)

Evk
  • 98,527
  • 8
  • 141
  • 191
0

Tested on Debian;

        DateTimeOffset parsedDate = new DateTimeOffset();
        if(DateTimeOffset.TryParse("29.10.2020".Replace(".", "/"), out parsedDate))
        {
            Console.WriteLine(parsedDate.ToString());
        }
        else
        {
            Console.WriteLine("Failed to parse");
        }

or as suggested above

        DateTimeOffset parsedDate = new DateTimeOffset();
        if(DateTimeOffset.TryParseExact("29.10.2020", "dd.MM.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate))
        {
            Console.WriteLine(parsedDate.ToString());
        }
        else
        {
            Console.WriteLine("Failed to parse");
        }
Xavier
  • 1,383
  • 7
  • 6