karal kunal
karal kunal

Reputation: 3

how to get the difference of time between two timezones along with day light saving

I need to get the difference between two time zones dynamically based on the user's profile.

Suppose if I am user A and today my profile is in MST time zone which is source time zone and my destination time zone is in PST time zone, then I need to get the difference of hrs of both MST and PST. Let us assume 3 hrs is the difference then I need to get the difference of these three hrs.

At the same time another user B may be in in CST time zone which is source time zone and the destination time zone is in HST time zone, then I need to get the difference of hrs of both CST and HST.Let us assume 1 hr is the difference then I need to get the difference of this one hr.

If 'N' no of users are using the application and if they are in daylight saving then time zone calculation should be done based on daylight saving if they don't have day light saving the time zone calculation should be done without considering day light.

How can I implement this? Can anybody help me out as I am new to this.

Upvotes: 1

Views: 883

Answers (2)

Jason W
Jason W

Reputation: 13179

We have handled this in the past using the DateTimeOffset parsed either

  • JavaScript inserting the local time of the user during form submission
  • User profile in which we save the preferred timezone offset in minutes

We have stored the offset as SMALLINT instead of alternatives because it is so easy to work with TimeSpan.FromMinutes and so we can support time zones not just an hour offset (like India being UTC+5:30).

With the DateTimeOffset stored in the database with the exact UTC offset, we've been able to report as we prefer (by UTC or translating to a preferred timezone) or show audit/data tables meeting our requirements.

With that said, I imagine there are better and many other approaches.

Upvotes: 0

Ross Bush
Ross Bush

Reputation: 15155

This mechanism has worked for me for years. There is a registry key that Windows uses to maintain the time zone info of the local machine. I can't remember the version, however, a few years back .NET wrapped reading the time zone info into TimeZoneInfo objects, prior to that you had to write a wrapper yourself. The psuedo below shows the general usage. The basic tenants are:

1. Each client receives the time from the DB or other Layer as UTC. (all datetimes saved as UTC)
2. Using the client profile, convert the UTC DateTime value to the client's local value.
3. Display the date and do date math in the user's local time.
4. Before sending the time to the DB or another layer with different TZ convert back to UTC.

This should keep your date logic sound.

string userTimeZone = GetUsersTimeZone();
DateTime timeReadFromDBOrSomewhereInUTC = GetSomeDateTimeInUTC();


DateTime timeInUsersTimeZone = FromUTCToSpecificTimeZone(userTimeZone ,timeReadFromDBOrSomewhereInUTC );

edtTimeForAppointment.Text = timeInUsersTimeZone.ToString();

timeInUsersTimeZone.AddHours(2);

DateTime timeConvertedToUTCToSaveToDB = FromSpecificTimeZoneToUTC(userTimeZone,timeInUsersTimeZone);

Here is an example of two functions using TimeZoneInfo.

public static DateTime FromSpecificTimeZoneToUTC(TimeZoneInfo fromZone, DateTime specificTimeZoneDateTime)
{
    DateTime temp = DateTime.SpecifyKind(specificTimeZoneDateTime, DateTimeKind.Unspecified);
    return TimeZoneInfo.ConvertTimeToUtc(temp, fromZone);
}

public static DateTime FromUTCToSpecificTimeZone(TimeZoneInfo toZone, System.DateTime UTCTimeZoneDateTime)
{           
    return TimeZoneInfo.ConvertTimeFromUtc(UTCTimeZoneDateTime, toZone);
}

If you just need the offset of a DateTime between two timrzones then the function below may prove useful.

public static TimeSpan GetTimeZoneOffsetDifference(TimeZoneInfo oldZone, TimeZoneInfo newZone)
{           
    var now = DateTimeOffset.UtcNow;
    TimeSpan oldOffset = oldZone.GetUtcOffset(now);
    TimeSpan newOffset = newZone.GetUtcOffset(now);
    TimeSpan difference = oldOffset - newOffset;
    return difference;
}

Upvotes: 1

Related Questions