DEV Community

loading...

DateTime.TryParse and the case of Z letter

Bohdan Stupak
Originally published at wkalmar.github.io ・4 min read

Recently I've been tasked to provide date in a specific format from backend to the frontend and I've noticed a behavior that I've found a bit odd.

private static void OutputDateInfo(string value)
{
    Console.WriteLine($"Input: {value}");
    if (DateTime.TryParse(value, out DateTime dateTimeValue))
    {
        Console.WriteLine($"Setialized to universal format {dateTimeValue.ToString("yyyy-MM-dd'T'HH:mm:ssZ")}");
    }
    Console.WriteLine();
}

//somewhere in main
OutputDateInfo("2021-04-15T21:01:00.0000000Z");
OutputDateInfo("2021-04-15T21:01:00.0000000");
Enter fullscreen mode Exit fullscreen mode

Produces the output

Input: 2021-04-15T21:01:00.0000000Z
Setialized to universal format 2021-04-16T00:01:00Z

Input: 2021-04-15T21:01:00.0000000
Setialized to universal format 2021-04-15T21:01:00Z
Enter fullscreen mode Exit fullscreen mode

So as I was in Kyiv which is in UTC+3 timezone it looked as letter Z forced ToString method to convert date to local time.

As documentation reads

If s contains no time zone information, result contains a DateTime value whose Kind property is DateTimeKind.Unspecified when the method returns. If the string to be parsed contains time zone information, result contains a DateTime value whose Kind property is DateTimeKind.Local when the method returns.

Is this DateTimeKind that makes ToString cast date to the local timezone? Let's log more properties to see if it is the case

private static void OutputDateInfo(string value)
{
    Console.WriteLine($"Input: {value}");
    if (DateTime.TryParse(value, out DateTime dateTimeValue))
    {
        Console.WriteLine($"Setialized to universal format {dateTimeValue.ToString("yyyy-MM-dd'T'HH:mm:ssZ")}");
        Console.WriteLine($"Setialized to default format {dateTimeValue}");
        Console.WriteLine($"Setialized with conversion to universal time {dateTimeValue.ToUniversalTime()}");
        Console.WriteLine($"Kind: {dateTimeValue.Kind}");
    }
    Console.WriteLine();
}
Enter fullscreen mode Exit fullscreen mode

Now we see in a console:

Input: 2021-04-15T21:01:00.0000000Z
Setialized to universal format 2021-04-16T00:01:00Z
Setialized to default format 16.04.2021 0:01:00
Setialized with conversion to universal time 15.04.2021 21:01:00
Kind: Local

Input: 2021-04-15T21:01:00.0000000
Setialized to universal format 2021-04-15T21:01:00Z
Setialized to default format 15.04.2021 21:01:00
Setialized with conversion to universal time 15.04.2021 18:01:00
Kind: Unspecified
Enter fullscreen mode Exit fullscreen mode

So indeed when we call ToString on a DateTimeKind.Local instance it will be adjusted according to server timezone.

But why letter Z is treated as timezone information? The documentation referred above has no example with Z letter. The answer is that format in question is ISO-8601 format and Z stands for "Zero UTC offset". Does string of this format is treated just as any other string with timezone specified or is this some kind of special treatment? We'll get an answer by comparing them.

OutputDateInfo("2021-04-15T21:01:00.0000000Z");
OutputDateInfo("2021-04-15T21:01:00.0000000");
OutputDateInfo("2021-04-15T14:01:00.0000000 -7:00");
Enter fullscreen mode Exit fullscreen mode

yeilds

Input: 2021-04-15T21:01:00.0000000Z
Setialized to universal format 2021-04-16T00:01:00Z
Setialized to default format 16.04.2021 0:01:00
Setialized with conversion to universal time 15.04.2021 21:01:00
Kind: Local

Input: 2021-04-15T21:01:00.0000000
Setialized to universal format 2021-04-15T21:01:00Z
Setialized to default format 15.04.2021 21:01:00
Setialized with conversion to universal time 15.04.2021 18:01:00
Kind: Unspecified

Input: 2021-04-15T14:01:00.0000000 -7:00
Setialized to universal format 2021-04-16T00:01:00Z
Setialized to default format 16.04.2021 0:01:00
Setialized with conversion to universal time 15.04.2021 21:01:00
Kind: Local
Enter fullscreen mode Exit fullscreen mode

As you can see string of ISO-8601 format is treated just as any other date string with timezone specified.

My intent though wasn't to treat it as local time but instead as UTC time. My goal was to provide universal time to front end so it will adjust it to end-user's local time. How can we trick .NET into thinking that this is universal time without any timezone? The answer is the overload that accepts DateTimeStyles:

private static void OutputDateInfo(string value)
{
    Console.WriteLine($"Input: {value}");
    if (DateTime.TryParse(value, null, DateTimeStyles.AdjustToUniversal, out DateTime universalTime))
    {
        Console.WriteLine($"Adjusted to universal {universalTime}");
    }

    Console.WriteLine();
}
Enter fullscreen mode Exit fullscreen mode

Outputs

Input: 2021-04-15T21:01:00.0000000Z
Adjusted to universal 15.04.2021 21:01:00

Input: 2021-04-15T21:01:00.0000000
Adjusted to universal 15.04.2021 21:01:00

Input: 2021-04-15T14:01:00.0000000 -7:00
Adjusted to universal 15.04.2021 21:01:00
Enter fullscreen mode Exit fullscreen mode

Job done!

Conclusion

In my opinion, DateTimeKind is underrepresented in literature and blogs. However, this is the thing that should be definitely taken into account when parsing date strings and converting dates back to string. Also, it worth remembering that ISO-8601 format date strings are treated just as any other string with timezone info specified, although it might be unobvious at the first glance.

Discussion (0)