日期时间到字符串时区 [英] datetime to string with time zone

查看:176
本文介绍了日期时间到字符串时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已存储在通用时间的DateTime值(UTC)的 2010-01-01 1时01分01秒

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

我想在EST这种格式的显示它2010-01-01 04:01:01GMT,04:00 ,但是'K'格式化的时区不工作的ToString

I would like to display it in EST in this format 2010-01-01 04:01:01GMT-04:00, however the 'K' formatter for timezone doesn't work in ToString

推荐答案

使用的ZZZ格式说明,以获得UTC偏移量。例如:

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);

输出:
2009-12-31 19点01分01秒格林尼治标准​​时间06:00

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

我在CDT时区。确保日期时间是明确DateTimeKind.Utc。

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

这篇关于日期时间到字符串时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆