From the documentation:
With DateTime
values, the "zzz" custom format specifier represents the signed offset of the local operating system's time zone from UTC, measured in hours and minutes. It does not reflect the value of an instance's System.DateTime.Kind
property. For this reason, the "zzz" format specifier is not recommended for use with DateTime
values.
Instead, either use DateTimeOffset
values (in which "zzz" does what you think it should), or if you continue to use DateTime
values then use the "K" specifier.
For example, on my computer (which is in the US Pacific time zone):
DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:sszzz") // "2017-06-21T14:57:17-07:00"
DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssK") // "2017-06-21T14:57:17Z"
DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:sszzz") // "2017-06-21T14:57:17+00:00"
- On line 1, even though the time is the UTC time, the offset is incorrectly showing local time.
- On line 2, the
K
specifier picks up on the UTC kind and properly gives a Z
in the result.
- On line 3, the zero offset is properly conveyed by the
zzz
specifier.
Related: https://stackoverflow.com/a/31223893/634824