datetime to string with time zone

前端 未结 5 820
礼貌的吻别
礼貌的吻别 2020-11-30 03:50

I have a DateTime stored in universal time (UTC) of value 2010-01-01 01:01:01.

I would like to display it in EST in this format 2010-01-01 0

5条回答
  •  一个人的身影
    2020-11-30 03:57

    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.

提交回复
热议问题