Conversion of DateTime value from Server to Client machine’s timezone

Author - Harleen Kaur

Conversion of datetime value from Server to Client machine’s timezone in C# .NET

There could be a requirement that date time field value from the server should be displayed in converted date-time based on the client machine’s time zone.

Using the client-side scripting language like javascript/jquery, we could find the plugins that deal with such conversions.

But I had a case where I needed to fetch the date at the server, perform immediate conversion at the server side in C# code in web forms application and display on the browser screen. In such scenarios, where you need to perform the server-side conversion, you will somehow need to pass the client time zone info to the server in advance. In javascript, there is a method available that provides client machine time zone information by the offset from the Coordinated Universal time UTC/GMT time.

 var current_date = new Date();  
// Convert minutes Offset in hours offset
 var utc_offset_hours = current_date.getTimezoneOffset() / 60;  
 utc_offset_hours = (-1) * utc_offset_hours;

We can pass the client machine UTC offset information from the Url parameters as follows :

“/MyPage.aspx?ClientUtcOffset=" + encodeURIComponent(utc_offset_hours))”

Where at the server side in the page load event, fetch the URL query string parameters as follows:

  string  _clientUtcOffset =””;
  protected void Page_Load(object sender, EventArgs e)
      NameValueCollection nv = this.Request.QueryString;
       if (nv.AllKeys.Contains("ClientUtcOffset"))
                 _clientUtcOffset = nv["ClientUtcOffset"];           

Here we have now information of client time zone machine by the time zone offset in hours at the server side. Now at the server side in c# we have useful TimeZoneInfo class in .Net that comes up with a variety of methods to deal with the time zones. You can convert to date time to any time zone using this class method either by offset approach or by using TimeZoneInfo class objects as arguments.

As per my requirement, I am using the offset approach to convert the date time string to the client machine time zone.

Convert the server date time string value to date time based on client machine time zone

//Convert your date time string value to c# datetime object.
DateTime myDateTime= Convert.ToDateTime();      
//Convert the _clientUtcOffset Parameter to double data type for further processing by TimeZoneInfo class.
double clientUtcHoursOffset = Convert.ToDouble(_clientUtcOffset);   
clientUtcHoursOffset = Math.Round(clientUtcHoursOffset, 2);
// Convert the mydatetime object to UTC date time using ConvertTimeToUtc of TimeZoneInfo Class
DateTime utcDateTime = TimeZoneInfo.ConvertTimeToUtc(myDateTime);
// Add the client machine time offset hours from UTC time to the UTC converted DateTime object value 
DateTime modifieddate= utcDateTime.AddHours(clientUtcHoursOffset);
TBdate.Text = modifieddate.ToShortDateString()+" "+ modifieddate.ToShortTimeString();

Important considerations while dealing with Time Zone Conversions :

An important factor that plays an important role while calculating the time zone based on the client machine is Daylight Savings. Getting the offset from the current date using the javascript getTimezoneOffset() gives the current offset based on the current date time(in accordance with daylight savings ). Today date is 27th August 2018 for observation I changed my system time zone to Alaska (Utc : 9:00). At this time, Alaska is under daylight savings ( and Clock is currently 1 hour forward). On browser/client-side code -8 offsets is returned for Alaska at this time. When I changed the time to January, Alaska time is not under Daylight Savings time, so it gave me -9 offsets for Alaska. In general, it gives offset value according to the current clock time of Alaska on the client side (which is required in our case). Secondly, If you observe, On windows, it shows the option“Adjust for daylight saving time automatically”– On in the System Time date settings for the current date(27th August 2018) by default, and it shows the correct time as shown in the google for Alaska .

Conversion of datetime value from Server to Client

But If we manually set the option “Adjust for daylight saving time automatically” – Off in the Date Time settings, then it shows the System time 1 hour behind of time that is shown in google for Alaska. Similarly, results from date time conversion will also vary by 1-hour difference as the client side offset value will not change by systems daylight manual settings. So the above-specified code will work in a case with an offset if we do not manually change the default option from System date time settings.

You can also find the whether daylight savings is on/off on client machine( one of the help links for detecting the daylight savings on/off is )

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *