Is there an easy way to create a list of only the time zones in US? I am using this code but it produces every time zone and I only want Pacific, Central, Mountain, and Eastern
public List<TimeZoneInfo> _timeZones = TimeZoneInfo.GetSystemTimeZones().ToList();
There's little point trying to drink from the fire hose here. Just ask for the ones you want explicitly:
var zones = new List<TimeZoneInfo> {
TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
};
Don't forget Hawaii and Alaska :)
Well you could just filter them by ID using exactly the criteria you've described:
using System;
using System.Linq;
class Test
{
static void Main()
{
var inclusions = new[] { "Pacific", "Central", "Mountain", "Eastern" };
foreach (var zone in TimeZoneInfo.GetSystemTimeZones()
.Where(zone => inclusions.Any(x => zone.Id.Contains(x))))
{
Console.WriteLine(zone.Id);
}
}
}
However, that gets the following list:
Pacific Standard Time (Mexico)
Pacific Standard Time
US Mountain Standard Time
Mountain Standard Time (Mexico)
Mountain Standard Time
Central America Standard Time
Central Standard Time
Central Standard Time (Mexico)
Canada Central Standard Time
SA Pacific Standard Time
Eastern Standard Time
US Eastern Standard Time
Central Brazilian Standard Time
Pacific SA Standard Time
SA Eastern Standard Time
Central Europe Standard Time
Central European Standard Time
W. Central Africa Standard Time
Central Asia Standard Time
N. Central Asia Standard Time
AUS Central Standard Time
AUS Eastern Standard Time
West Pacific Standard Time
Central Pacific Standard Time
... which is obviously not quite what you want.
If you only want a specific set of time zones, you're probably best just hard-coding the IDs of the ones you want, using TimeZoneInfo.FindSystemTimeZoneById(id).
I created a list of US time zones:
var zones = new List<TimeZoneInfo> {
TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("US Mountain Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("Alaskan Standard Time"),
};
I think this is an updated list of US timezones:
var timezones = new List<TimeZoneInfo> {
TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("US Mountain Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("Hawaiian Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("Alaskan Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time (Mexico)"),
TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time (Mexico)"),
TimeZoneInfo.FindSystemTimeZoneById("UTC")
};
What about this?
var _timeZones = TimeZoneInfo.GetSystemTimeZones().ToList().Where(zone=>zone.DisplayName.Contains("US"));
Related
This question already has an answer here:
TimeZoneInfo.ConvertTime from PST to UTC to AEST - off by one hour
(1 answer)
Closed 4 years ago.
We are downloading data from our trading partner's API. Till now we were working with "E. Australia Standard Time" and it was working fine.
After daylight saving started our trading partner said that they are working with "Australian Eastern Daylight Time (AEDT)".
I have used following code to convert from UTC to "E. Australia Standard Time".
DateTime utcTime = DateTime.UtcNow;
TimeZoneInfo objTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("E. Australia Standard Time");
DateTime TPDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, objTimeZoneInfo);
But I am getting following errors when I use "E. Australia Daylight Time" or "Australian Eastern Daylight Time (AEDT)"
The time zone ID 'E. Australia Daylight Time' was not found on the local computer.
The time zone ID 'Australian Eastern Daylight Time (AEDT)' was not found on the local computer.
What timezone id should I pass to FindSystemTimeZoneById() method to convert correctly to Australian Eastern Daylight Time (AEDT)?
You probably want to use "AUS Eastern Standard Time" (for Canberra, Hobart, Melbourne and Sydney). Despite having the word "Standard" in the name, this accounts for daylight savings time and uses UTC+10 in winter and UTC+11 in summer:
var tz = TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time");
TimeZoneInfo.ConvertTimeFromUtc(new DateTime(2018,1,1,0,0,0,DateTimeKind.Utc), tz);
// => 01/01/2018 11:00:00
TimeZoneInfo.ConvertTimeFromUtc(new DateTime(2018,7,1,0,0,0,DateTimeKind.Utc), tz);
// => 01/07/2018 10:00:00
The "E. Australia Standard Time" time zone is for Brisbane, where they do not observe daylight savings time.
You can get a complete list of available time zones using the TimeZoneInfo.GetSystemTimeZones() method, or by running tzutil /l at the command line.
I'm trying to convert time between two timezones, and found out an hour difference between US Eastern Standard Time to Western European Time
Supposedly, USA Eastern Standard Time (EST) 2018 Jun-18 1PM should be Western European Time (WET) same day 6PM, but the result from c# ConvertTime is 7PM, I think I missed something for the daylight setting?
Anyway, here's the code:
var str = "2018-07-09T13:00:00";
var dt = Convert.ToDateTime(str);
var SourceZoneValue = "Eastern Standard Time";
var DestinationZoneValue = "W. Europe Standard Time";
TimeZoneInfo sourceTimeZone = TimeZoneInfo.FindSystemTimeZoneById(SourceZoneValue);
TimeZoneInfo destinationTimeZone = TimeZoneInfo.FindSystemTimeZoneById(DestinationZoneValue);
DateTime localTime = TimeZoneInfo.ConvertTime(dt, sourceTimeZone, destinationTimeZone);
Console.WriteLine(localTime);
The outcome is 7PM instead 6PM, any idea? tks
You want to convert from Eastern Standard Time to W. Europe Standard Time, say from New York to Amsterdam.
New York time zone is -5 GMT, and Amsterdam time zone is +1 GMT:
New York Daylight saving started 11 March 2018 and will end on 4 November 2018.
Amsterdam Daylight saving started 25 March 2018 and will end on 28 October 2018.
Your date is 18 June, so daylight saving does not affect the time difference, I would say 7 PM is the correct result.
Maybe W. Europe Standard Time is not the correct time zone that you are looking for? For example, for the UK time:
var britishZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
I have a code where I need to find current offset from UTC for Central European Timezone.
My code is deployed in azure app service.
var offset = DateTimeOffset.Now.Offset.Hours + ":" +
DateTimeOffset.Now.Offset.Minutes;
The problem with above code is, it relies on server time, Even though my App Service is in West Europe, the time zone of Azure App Service is always UTC .
One solution is to change Azure App Service TimeZone to Desired TimeZone and it will work, but i am also looking at getting the offset using code.
Remember I cannot use system datetime to get offset as it's always UTC.
Can I get Current Central Europe DatetimeOffset irrespective of system date time?
You can do something like this
TimeZoneInfo cet = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
DateTimeOffset offset = TimeZoneInfo.ConvertTime(DateTime.Now, cet);
As described here.
If you're not sure about a TimeZoneId you can use GetSystemTimeZones() to find it.
An alternative, as described here, would be to do something like this
DateTime nowCet = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now,
"Central European Standard Time");
The nice thing about this is you can easily calculate the difference between two time zones like, for example
DateTime newYork = new DateTime(2017, 10, 04, 12, 23, 00);
DateTime berlin = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(newYork,
"Eastern Standard Time", "Central European Standard Time");
TimeSpan diff = berlin - newYork;
You can use TimeZoneInfo:
TimeZoneInfo cetInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");
DateTimeOffset cetTime = TimeZoneInfo.ConvertTime(DateTimeOffset.Now, cetInfo);
With correct daylight saving offsets (example in CET):
DateTime utcDT = DateTime.UtcNow;
DateTime cetDT = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcDT, "UTC", "Central European Standard Time");
DateTimeOffset utcDTO = new DateTimeOffset(utcDT);
DateTimeOffset cetDTO = new DateTimeOffset(cetDT, cetDT - utcDT);
// Result (with daylight saving)
//
// 2021. 06. 24. 7:42:09
// 2021. 06. 24. 9:42:09
// 2021. 06. 24. 7:42:09 +00:00
// 2021. 06. 24. 9:42:09 +02:00
I am trying to convert to a swedish timezone with this code:
Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");
TimeZoneInfo cet = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
DateTime currentDate = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local);
var swedishTime = TimeZoneInfo.ConvertTime(currentDate, cet, TimeZoneInfo.Local);
For some reason I am getting:
{"The conversion could not be completed because the supplied DateTime
did not have the Kind property set correctly. For example, when the
Kind property is DateTimeKind.Local, the source time zone must be
TimeZoneInfo.Local.\r\nParameter name: sourceTimeZone"}
What am i missing?
A few things:
Culture only affects output formatting when converting to/from strings. It doesn't affect time zone conversions, so it is unnecessary here.
The time zone identifiers used by TimeZoneInfo on Windows come from the Windows operating system itself, and sometimes their names do not match up to what you may expect.
You're using the Windows time zone ID "Central European Standard Time", which has a display name of "(UTC+01:00) Sarajevo, Skopje, Warsaw, Zagreb".
For Sweden, you should actually use the ID "W. Europe Standard Time", which has a display name of "(UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna"
You can read more about this in the section titled "The Microsoft Windows Time Zone Database" in the timezone tag wiki.
Since it appears you are looking for the current time in a particular time zone, you should not be going through the local time zone at all. Just convert directly from UTC to the target time zone.
The code should simply be:
var tz = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
var swedishTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, tz);
Or if you prefer, you can use a convenience method:
var swedishTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow,
"W. Europe Standard Time")
Just delete "TimeZoneInfo.Local" from "var swedishTime".
Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");
TimeZoneInfo cet = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
DateTime currentDate = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local);
var swedishTime = TimeZoneInfo.ConvertTime(currentDate, cet);
I faced the same issue and I fixed it by changing to DateTimeKind.Unspecified.
Instead of this
var currentDateTime = DateTime.UtcNow;
I put this:
var currentDateTime = new DateTime(DateTime.UtcNow.Ticks, DateTimeKind.Unspecified);
My database has GMT Diff for every country like GMT+02:00 and GMT-04:00
Using C# .net is there a simple way to convert that to an actual an .Net timezone?
e.g. "Eastern Standard Time" or "E. Australia Standard Time"
thanks
You can find a timezone, just not that likely the one you want. The UTC offset is ambiguous. GMT+02:00 is the offset for
Jordan Standard Time
GTB Standard Time
Middle East Standard Time
Egypt Standard Time
Syria Standard Time
South Africa Standard Time
FLE Standard Time
Israel Standard Time
E. Europe Standard Time
Any you like in particular? You'd get them with an expression like this:
public static TimeZoneInfo[] GetTimeZones(TimeSpan offset) {
return TimeZoneInfo.GetSystemTimeZones().Where(z => z.BaseUtcOffset == offset).ToArray();
}
I had to look some of these up. FLE = Finland, Lithuania, Estonia. GTB is tougher, I'm guessing at Greece, Turkey, Bulgaria.
That's not going to be possible because different time zones can share the same offset from GMT (or UTC to be more correct).
If you're expecting "Eastern Standard Time" out of it, you need more information than just the offset from GMT. For example, GMT-5 is Eastern Standard Time, Central Daylight Time, Eastern Standard Time (Indiana), and various South American time zones, both with and without Daylight Saving, and on a different DST schedule than the US.
If you are ok with just the first available TimeZone which matches that GMT offset, that should be easy.
This is off my head but...
TimeZoneInfo getTimeZone(string gmtstring)
{
foreach(TimeZoneInfo ti in TimeZoneInfo.GetSystemTimeZones())
{
TimeSpan tsp = ti.BaseUtcOffset;
if(tsp.ToString((tsp.TotalMinutes < 0 ? "-" : "+")+"hh:mm") == gmtstring.Substring(3))
return ti;
}
return null;
}
Otherwise there's no direct relation between timezones and GMT offsets (there are many timezones on the same GMT offset, as the others said)
PS: gmtstring should be in the "GMT(+|-)HH:SS" format