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
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
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.
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)
O
format specifier (O
includes the colon and seems to have microsecond precision by default) –
Vasoinhibitor "yyyy-MM-ddTHH:mm:ss.fffffffzzz"
. –
Audi 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.
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"));
HH
, not hh
. –
Blakely t
/tt
should be added to make hh
meaningful (e.g. 02:00 AM
, 03:00 PM
), or hh
should be replaced with HH
to produce 24-hour format (e.g. 02:00
, 15:00
). So the yyyy-MM-dd hh:mm:ss
format produces ambiguous output that can't be decoded back :(. –
Blakely 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.
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.
© 2022 - 2025 — McMap. All rights reserved.
DateTime.UtcNow
, it doesn't show the offset, because you're already on the GMT timezone and instead 'Z' is appended to your string. But, if you call it on a local time, it shows the offset correctly. The code for ISO8601 format would beDateTime.Now.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'GMT'K", CultureInfo.InvariantCulture)
– Beggs