带时区的日期时间字符串

发布于 2024-09-10 20:58:59 字数 169 浏览 2 评论 0原文

我有一个以世界时 (UTC) 存储的日期时间,其值为 2010-01-01 01:01:01

我想以 EST 格式显示它 2010-01-01 04:01:01GMT-04:00,但是时区的“K”格式化程序在 ToString 中不起作用

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 04:01:01GMT-04:00, however the 'K' formatter for timezone doesn't work in ToString

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

微暖i 2024-09-17 20:58:59

使用“zzz”格式说明符获取 UTC 偏移量。例如:

        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 GMT-06:00

我在 CDT 时区。确保 DateTime 明确是 DateTimeKind.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);

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

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

听闻余生 2024-09-17 20:58:59

如果像我一样,您碰巧需要像 2018-03-31T01:23:45.678-0300 这样的格式(时区部分没有冒号),您可以使用:

datetime.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz").Remove(26,1)

If like myself you happen to need a format like 2018-03-31T01:23:45.678-0300 (no colon in the timezone part), you can use this:

datetime.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz").Remove(26,1)
似最初 2024-09-17 20:58:59

此方法将返回东部标准时间的指定时间(按照问题要求),即使 EST 不是本地时区

public string GetTimeInEasternStandardTime(DateTime time)
{
    TimeZoneInfo easternStandardTime = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    DateTimeOffset timeInEST = TimeZoneInfo.ConvertTime(time, easternStandardTime);
    return timeInEST.ToString("yyyy-MM-dd hh:mm:ss tt\" GMT\"zzz");
}

注意:我尚未在非英语操作系统中对此进行测试。请参阅 TimeZoneInfo.FindSystemTimeZoneById 上的 MSDN 文档。

This method will return the specified time in Eastern Standard Time (as the question requested), even if EST is not the local time zone:

public string GetTimeInEasternStandardTime(DateTime time)
{
    TimeZoneInfo easternStandardTime = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    DateTimeOffset timeInEST = TimeZoneInfo.ConvertTime(time, easternStandardTime);
    return timeInEST.ToString("yyyy-MM-dd hh:mm:ss tt\" GMT\"zzz");
}

Note: I haven't tested this in a non-English OS. See the MSDN documentation on TimeZoneInfo.FindSystemTimeZoneById.

欢你一世 2024-09-17 20:58:59

像这样的东西有效。你可能可以再清理一下:

string newDate = string.Format("{0:yyyy-MM-dd HH:mm:ss} GMT {1}", dt.ToLocalTime(), dt.ToLocalTime().ToString("%K"));

Something like this works. You could probably clean it up a bit more:

string newDate = string.Format("{0:yyyy-MM-dd HH:mm:ss} GMT {1}", dt.ToLocalTime(), dt.ToLocalTime().ToString("%K"));
苏辞 2024-09-17 20:58:59

我认为您正在寻找 TimeZoneInfo 类(请参阅http://msdn.microsoft.com/en-us/library/system.timezoneinfo_members.aspx)。它有许多静态方法来在时区之间转换日期。

I think you are looking for the TimeZoneInfo class (see http://msdn.microsoft.com/en-us/library/system.timezoneinfo_members.aspx). It has many static methods to convert dates between time zones.

一片旧的回忆 2024-09-17 20:58:59

如果您使用“zzz”格式说明符为您提供相对于 UTC 的本地时区偏移量,但如果您想要获得相对于 UTC 的特定时区偏移量,则应使用 ToOffset 方法。

在此处查看更多信息

ToOffset 方法是调用 TimeZoneInfo.ConvertTime(DateTimeOffset, TimeZoneInfo) 方法的替代方法。当时区与协调世界时 (UTC) 的偏移已知时,它对于执行从一个时区到另一个时区的简单转换非常有用。

例如

    Console.WriteLine("Local time zone :" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"));
    Console.WriteLine("Iran time zone :" + ((DateTimeOffset)DateTime.Now).ToOffset(TimeZoneInfo.FindSystemTimeZoneById("Iran Standard Time").BaseUtcOffset).ToString("yyyy-MM-ddTHH:mm:sszzz"));
    Console.WriteLine("Eastern time zone :" + ((DateTimeOffset)DateTime.Now).ToOffset(TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time").BaseUtcOffset).ToString("yyyy-MM-ddTHH:mm:sszzz"));

    //Outputs
    Local time zone :2024-06-21T01:41:15+03:30
    Iran time zone :2024-06-21T01:41:15+03:30
    Eastern time zone :2024-06-20T17:11:15-05:00

,在上面的示例中,第一行的输出可能会通过更改 Local TimeZone 设置而改变,但接下来两行的输出始终相同,并且基于特定的给定时区。

If you use the "zzz" format specifier give you the local time zone offset from UTC, but if you want have the specific time zone offset from UTC you should using ToOffset method.

see more here

The ToOffset method is an alternative to calling the TimeZoneInfo.ConvertTime(DateTimeOffset, TimeZoneInfo) method. It can be useful for performing simple conversions from one time zone to another when the time zones' offsets from Coordinated Universal Time (UTC) are known.

For example

    Console.WriteLine("Local time zone :" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"));
    Console.WriteLine("Iran time zone :" + ((DateTimeOffset)DateTime.Now).ToOffset(TimeZoneInfo.FindSystemTimeZoneById("Iran Standard Time").BaseUtcOffset).ToString("yyyy-MM-ddTHH:mm:sszzz"));
    Console.WriteLine("Eastern time zone :" + ((DateTimeOffset)DateTime.Now).ToOffset(TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time").BaseUtcOffset).ToString("yyyy-MM-ddTHH:mm:sszzz"));

    //Outputs
    Local time zone :2024-06-21T01:41:15+03:30
    Iran time zone :2024-06-21T01:41:15+03:30
    Eastern time zone :2024-06-20T17:11:15-05:00

In the example above, the output of the first line may change by changing the Local TimeZone setting, but the output of the next two lines is always the same and based on the specific given time zone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文