与时区连接的时间时间

时间:2021-02-03 21:32:31

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

我有一个日期时间存储在universal time (UTC)值2010-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

我想以这种格式(2010-01-01 04:01:01 gm -04:00)显示它,但是时区的“K”格式化程序不能在ToString中工作

5 个解决方案

#1


49  

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

使用“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);

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

输出:2009-12-31 19:01:01 GMT-06:00

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

我在CDT时区。确保DateTime是明确的DateTimeKind.Utc。

#2


3  

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

#3


1  

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.

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

#4


1  

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

此方法将返回东部标准时间的指定时间(如问题要求),即使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");
}

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

注意:我没有在非英语操作系统中测试过这个。请参阅TimeZoneInfo.FindSystemTimeZoneById上的MSDN文档。

#5


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:

如果你碰巧需要一种格式,比如2018-03-31t01:23 . 23:48 . 708 -0300(在时区部分没有冒号),你可以用这个:

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

#1


49  

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

使用“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);

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

输出:2009-12-31 19:01:01 GMT-06:00

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

我在CDT时区。确保DateTime是明确的DateTimeKind.Utc。

#2


3  

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

#3


1  

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.

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

#4


1  

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

此方法将返回东部标准时间的指定时间(如问题要求),即使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");
}

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

注意:我没有在非英语操作系统中测试过这个。请参阅TimeZoneInfo.FindSystemTimeZoneById上的MSDN文档。

#5


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:

如果你碰巧需要一种格式,比如2018-03-31t01:23 . 23:48 . 708 -0300(在时区部分没有冒号),你可以用这个:

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