Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting time from different timezone with daylight savings included

Tags:

c#

.net

datetime

I have process that accepts datetime value with timezone as a string (data comes from outside system). I need to translate this datetime to what the time would have been at the local machines timezone.

Example code:

string cetId = "Central European Standard Time";
if (timeZone == "CET")
{
    TimeZoneInfo cetZone = TimeZoneInfo.FindSystemTimeZoneById(cetId);
    returnDateTime = TimeZoneInfo.ConvertTime(statusDateTime, cetZone, TimeZoneInfo.Local);
}
else if (timeZone == "CEST")
{
    TimeZoneInfo cestZone = TimeZoneInfo.FindSystemTimeZoneById(cetId);
    returnDateTime = TimeZoneInfo.ConvertTime(statusDateTime, cestZone, TimeZoneInfo.Local);
}

Do I need to do anything specific if the time is CEST (central european summer time) instead of CET (central european time) or does .net TimeZoneInfo object handle that scenario?

like image 694
Tadhg Avatar asked Mar 28 '13 17:03

Tadhg


People also ask

How to convert the current date and time to another time zone?

Here’s how to convert the current date and time from the local time zone to another one: [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId( ` (Get-Date), 'Greenwich Standard Time')

What are the parameters of the date and time conversion method?

This method's parameters are the date and time value to convert, the identifier of the date and time value's time zone, and the identifier of the time zone to convert the date and time value to.

How do I find the time difference between several cities?

Find the time difference between several cities with the Time Difference Calculator. Provides time zone conversions taking into account daylight saving time (DST), local time zone and accepts present, past or future dates. For current time anywhere in the world, please use The World Clock.

What is the difference between switchoffset and at Time Zone?

With SWITCHOFFSET (), you must provide the actual time zone offset either in the format [+|-]TZH:TZM or as a signed integer (for minutes). This means you need to know the exact time zone offset, and whether or not that time zone is currently observing daylight savings. With the AT TIME ZONE clause, you don’t need to know that.


1 Answers

You should be fine.

You are telling ConvertTime what both time zones (source and target) are.

Do you have a specific problem with this or are you just asking for confirmation?

like image 131
500 - Internal Server Error Avatar answered Sep 30 '22 17:09

500 - Internal Server Error