I have my local time in GMT +01:00 as the time of writing. I experience something, to me unexpected, when doing a ToString in the following way. Here we go:
Demonstrating the local system settings with +01:00 time zone (all these run green):
var myLocalDate = new DateTime(2020, 11, 25, 08, 00, 00, DateTimeKind.Local);
Assert.AreEqual("2020-11-25T08:00:00+01:00", myLocalDate.ToString(@"yyyy-MM-dd\THH:mm:sszzz"));
Assert.AreEqual(DateTimeKind.Local, myLocalDate.Kind);
Assert.AreEqual(myLocalDate, myLocalDate.ToLocalTime());
And now I create the same time, in utc, by manually subtracting an hour and specifying the "utc" as kind. But when I call ToString, the time zone is written as +01:00 which I would expect to be +00:00:
var myUtcDate = new DateTime(2020, 11, 25, 07, 00, 00, DateTimeKind.Utc);
// THIS Breaks:
Assert.AreEqual("2020-11-25T07:00:00+00:00", myUtcDate.ToString(@"yyyy-MM-dd\THH:mm:sszzz"));
Error message:
Message: Assert.AreEqual failed. Expected:<2020-11-25T07:00:00+00:00>. Actual:<2020-11-25T07:00:00+01:00>.
Do I miss something about datetimes and formats here, or is this maybe a known bug?
I run .Net Framework 4.8
This post is about the same issue, I see: How to solve DateTimeInvalidLocalFormat error: "A UTC DateTime is being converted to text in a format that is only correct for local times."?
UPDATE:
Running the following program yields different results in dotnet framework and dotnet core (as mentioned by evk):
Console.WriteLine(new DateTime(2025, 11, 25, 07, 00, 00, DateTimeKind.Utc).ToString(@"yyyy-MM-dd\THH:mm:sszzz"));
dotnet core prints:
2020-11-25T07:00:00+00:00
dotnet framework prints:
2020-11-25T07:00:00+01:00
Furthermore, when running dotnet framework in debug mode, the following debug assistant message shows up, but is ignored internally in the DateTime.ToString():
Managed Debugging Assistant 'DateTimeInvalidLocalFormat' : 'A UTC DateTime is being converted to text in a format that is only correct for local times. This can happen when calling DateTime.ToString using the 'z' format specifier, which will include a local time zone offset in the output. In that case, either use the 'Z' format specifier, which designates a UTC time, or use the 'o' format string, which is the recommended way to persist a DateTime in text. This can also occur when passing a DateTime to be serialized by XmlConvert or DataSet. If using XmlConvert.ToString, pass in XmlDateTimeSerializationMode.RoundtripKind to serialize correctly. If using DataSet, set the DateTimeMode on the DataColumn object to DataSetDateTime.Utc. '
zzz
is valid as a format specifier, as documented in the very page you linked to. I don't know what you were trying to say in the comment, but I believe it was unclear, incorrect or both.