Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives

How to: Calculate the time difference between the internet client and your ASP.Net application

At my current project we use aspNetEmail for sending emails. One of our applications allow the users to register for a training course. We use the component to send invitations for Outlook and Exchange clients which automatically add the training course to the calendar.

The begin and end date for a calendar entry need to be defined in UniversalTime which is not a problem when you send invitations to users in the same time zone as the server that the application is running on. But we have users in various time zones in the US, in Russia, India and soon Malaysia, Singapore and Nigeria. In tests we noticed that the invitations we were sending to our users in those time zones were getting the wrong date and time once they were entered in their Outlook or Exchange calendar. So we needed to figure out a way to calculate the difference between the server time zone and the time zone the user was running. Here's how we solved it.

We first wrote a small piece of Java script which we included in the HEAD section of our ASPX page. The Java function returns the offset of the client time zone compared to the UTC.

function GetClientUTC()
{    
    var now = new Date();
    var offset = now.getTimezoneOffset();
    document.Form1.hdClientUTC.value = offset;
}

We then added an onload tag to the body section of the ASPX page:

<body MS_POSITIONING="GridLayout" onload="GetClientUTC()">

And because we are a little paranoid that users may change their time zone before they submit their registration for our training course we did the same for the onsubmit tag in the form section of the ASPX page:

<form id="formRegistration" method="post" runat="server" onsubmit="GetClientUTC()">

So we now have piece of Java code of which we are certain that it will assign the clients TimeZoneOffset value to a variable in our ASPX page. Which we need to define of course. For this we added the following line of code to our ASPX code behind file.

protected System.Web.UI.HtmlControls.HtmlInputHidden hdClientUTC;

We now know what the offset to the UTC is for our client. But in order to calculate the correct date and time for this time zone when we send invitations we need to know what the UTC offset for the webserver is:

private static int GetUtcOffset()
{
    TimeSpan local = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
 
    return (local.Hours * 60) + (local.Minutes);
}

We add this value to the UTC value we retrieved from the client. This now gives us a value in minutes which we can use to correctly calculate the invitation date and time for the time zone our user is located in: 

int UTCOffset = int.Parse(hdClientUTC.Value.ToString());
 
UTCOffset += GetUtcOffset();
 
// Initialise email message using aspNetEmail
// Create invitation
appointment.Event.DateStart.Date = start.ToUniversalTime().AddMinutes((double)UTCOffset);
appointment.Event.DateStart.IsUTCDateTime = true;
 
appointment.Event.DateEnd.Date = end.ToUniversalTime().AddMinutes((double)UTCOffset);
appointment.Event.DateEnd.IsUTCDateTime = true;

And that's it. We can now send correct invitations to all time zones.

Comments

Vivek said:

Hey thats gr8 Information, this might also be helpful dotnetguts.blogspot.com/.../understanding-datetime-and-timespan-in.html

# June 18, 2008 7:20 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)