How to convert UTC to Australian Eastern Daylight Time (AEDT)? [duplicate] - c#

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.

Related

TimeZoneInfo.ConvertTimeFromUtc is returning the wrong value

I'm taking a set date/time value and converting from UTC, but timezone. I'm expecting to get the original time - 5 to be the correct time in the EST time zone, but it is off by 1 hour and only offset it by 4 hours.
string dateString = "3/15/2021 6:59 AM";
DateTime TimeDataDue = DateTime.Parse(dateString, System.Globalization.CultureInfo.InvariantCulture);
TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var TimeDataDueEastern = TimeZoneInfo.ConvertTimeFromUtc(TimeDataDue, easternZone).Dump();
The output I get is "3/15/2021 2:59:00 AM", but I expect to get "3/15/2021 1:59:00 AM"
Any suggestions?
Thanks!
As Yarik said in the question comments, the result you obtained is correct.
You can review the 2021 DST schedule for the US Eastern Time zone here:
https://www.timeanddate.com/time/zone/usa/new-york?year=2021
Additionally, you'll note that easternZone.DisplayName in your code will be "(UTC-05:00) Eastern Time (US & Canada)" (assuming English). In other words, despite the time zone having the word "Standard" in its Id, it applies for the entire year including both EST and EDT periods.
Since EDT is in effect on March 15th 2021, you get a result that is four hours behind UTC.

C# TimeZoneInfo.ConvertTime incorrect result

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

Get Datetime Offset for Timezone

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

C# british summer time (BST) timezone abbreviation

I need to display a label with the current time zone abbreviation. My pc's timezone is currently set to "(GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London". As a result, I would like to see BST displayed, since LN is currently in british summer time.
It looks like that info (timezone abbrev) is not available. Looking at the GMT TimeZoneInfo, all I can see with regard to names is
Id "GMT Standard Time"
StandardName "GMT Standard Time"
DaylightName "GMT Daylight Time"
Is there any way to get to BST from "GMT Daylight Time" or any other available Windows timezone info?
The TimeZoneInfo class referrers to "British Summer Time" as "GMT Daylight Time", so no it is not possible. If Microsoft were to store it as "BST" it would be in the DaylightName property.
Whilst following the development of the TimeZoneInfo class on the BCL blog many years ago, I saw no explanation behind how they chose the values for DaylightName. If I were to guess it would be because this is for a "Time Zone" and not a particular city.
It appears that the public-domain tzdatabase, which is considered to be more complete than Microsoft's time zone database, does display BST for London (source). This is because Cities are included in this dataset, not just the Time Zones.
There is a Project called Noda Time that brings the tzdatabase to .Net that is now avaialble.
You should know that all the time zones that say "Standard" will properly switch to daylight time in the summer. For example TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time") will be an object that can properly handle both standard and daylight times. The same is true for, say, "Pacific Standard Time".
I think you have two choices: Special case handling for the GMT time zone, or generalized handling for "Time zones for which Microsoft uses an incorrect name."
I wonder, for example, whether the Portuguese use "GMT" for their time zone in the winter. Whether they do or not, I doubt they use "British Summer Time" in the summer!

Select only United State Time Zones

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

Categories