I'm using TimeZoneInfo.ConvertTimeFromUtc method to convert date time to the target date time. Current its UTC+10 in Sydney. If i run the code its perfectly converting time to UTC+10. But in Jan with out daylight saving It was UTC+11. If I input a Jan date run the code. Its still converting it as UTC+10 instead of UTC+11.
Below is the sample code.
TimeZoneInfo ESTZone = TimeZoneInfo.FindSystemTimeZoneById("E. Australia Standard Time");
DateTime date = Convert.ToDateTime("2019-01-23 13:15:23.6090752");
DateTime SydneyDateTime = TimeZoneInfo.ConvertTimeFromUtc(date, TimeZoneInfo.FindSystemTimeZoneById(Timezone));
string EST = SydneyDateTime.ToString() + " / " + SydneyDateTime.IsDaylightSavingTime().ToString() + " / " + ESTZone.IsDaylightSavingTime(SydneyDateTime)+" / " + ESTZone.DaylightName + " / " + ESTZone.StandardName;
DateTime IndiaDateTime = TimeZoneInfo.ConvertTimeFromUtc(date, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"));
DateTime date2 = Convert.ToDateTime("2019-05-23 13:15:23.6090752");
DateTime SydneyDateTime2 = TimeZoneInfo.ConvertTimeFromUtc(date2, TimeZoneInfo.FindSystemTimeZoneById(Timezone));
string EST2 = SydneyDateTime2.ToString() + " / " + SydneyDateTime2.IsDaylightSavingTime().ToString() + " / " + ESTZone.IsDaylightSavingTime(SydneyDateTime2) + " / " + ESTZone.DaylightName + " / " + ESTZone.StandardName;
Output Result:
EST = "23-01-2019 23:15:23 / False / False / E. Australia Summer Time / E. Australia Standard Time"
EST2="23-05-2019 23:15:23 / False / False / E. Australia Summer Time / E. Australia Standard Time"
In both the cases, its adding 10 hrs, but in January it should add 11 hrs.
Am I missing anything in the code.
You're using the wrong timezone. "E. Australia Standard Time"
corresponds to Brisbane time, and that timezone does not observe DST so your winter and summer times come back with the same UTC offset.
For Sydney, you need to use "AUS Eastern Standard Time"
. ConvertTimeFromUtc
will apply the UTC offset as appropriate for that timezone and give you the times you're expecting.
Here is your snippet including the use of the appropriate time zone, and a bit of editing around the Timezone initialisation so it compiled:
public void ConvertTimes()
{
string Timezone = "AUS Eastern Standard Time";
TimeZoneInfo ESTZone = TimeZoneInfo.FindSystemTimeZoneById(Timezone);
DateTime date = Convert.ToDateTime("2019-01-23 13:15:23.6090752");
DateTime SydneyDateTime = TimeZoneInfo.ConvertTimeFromUtc(date, TimeZoneInfo.FindSystemTimeZoneById(Timezone));
string EST = SydneyDateTime.ToString() + " / " + SydneyDateTime.IsDaylightSavingTime().ToString() + " / " + ESTZone.IsDaylightSavingTime(SydneyDateTime) + " / " + ESTZone.DaylightName + " / " + ESTZone.StandardName;
DateTime IndiaDateTime = TimeZoneInfo.ConvertTimeFromUtc(date, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"));
DateTime date2 = Convert.ToDateTime("2019-05-23 13:15:23.6090752");
DateTime SydneyDateTime2 = TimeZoneInfo.ConvertTimeFromUtc(date2, TimeZoneInfo.FindSystemTimeZoneById(Timezone));
string EST2 = SydneyDateTime2.ToString() + " / " + SydneyDateTime2.IsDaylightSavingTime().ToString() + " / " + ESTZone.IsDaylightSavingTime(SydneyDateTime2) + " / " + ESTZone.DaylightName + " / " + ESTZone.StandardName;
}
This gives the output:
EST = "24/01/2019 00:15:23 / False / True / AUS Eastern Daylight Time / AUS Eastern Standard Time"
EST2 = "23/05/2019 23:15:23 / True / False / AUS Eastern Daylight Time / AUS Eastern Standard Time"
which I think is what you were expecting.
Incidentally, DateTime.IsDaylightSavingTime() uses your local timezone to determine whether the time is within DST. It might be misleading looking at it here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With