datetime to string with time zone

Use the “zzz” format specifier to get the UTC offset. For example:

        var dt = new DateTime(2010, 1, 1, 1, 1, 1, DateTimeKind.Utc);
        string s = dt.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss \"GMT\"zzz");
        Console.WriteLine(s);

Output:
2009-12-31 19:01:01 GMT-06:00

I’m in the CDT timezone. Make sure the DateTime is unambiguously DateTimeKind.Utc.

Leave a Comment