Click here to Skip to main content
14,027,355 members
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 8 Sep 2006
Licenced CPOL

DateTime Values according to the remote time zone

, 8 Sep 2006
Rate this:
Please Sign up or sign in to vote.
Shows how to define timezone of the remote user and display date time values according to remote time zone


In the web applications we are often displaying date/time values. But how can we display value according to the user’s time zone?

We can do this by using following steps:

  • Get difference in hours between user’s local time and GMT using Java Script
  • Pass this value from client to the server
  • Attach this value to the remote user object on the server (for example attach it to the Profile object)
  • Detect actual time zone based on  difference between user’s local time and GMT.
  • Calculate time according to the time zone and display time and time zone

Code samples


Configuring storage of difference between local time and GMT is very simple in 2.0

<profile enabled="true" >
defaultValue="0" type="double"/>


Here is simple Javascript to get time difference on the browser.

<head runat="server">
<script type="text/javascript">
var rightNow = 
new Date();
var date1 = 
Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);

var temp = 
var date2 = 
new Date(temp.substring(0, 
temp.lastIndexOf(" ")-1));

var hoursDiffStdTime = 
(date1 - date2) / (1000 * 60 * 60);

createCookie("hoursDiffStdTime", hoursDiffStdTime, 0); 

createCookie(name,value,days) {
if (days && 
days > 0) {
var date = new Date();
var expires = 
"; expires="+date.toGMTString();
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
<body onload="GetLocalTimeOffset();">


Here is how we getting time difference on the server from the client, attaching it to the profile and passing it to calculate display values

Profile.hoursDiffStdTime = Double.Parse(Request.Cookies["hoursDiffStdTime"].Value ); 
Label1.Text = TimeZoneManager.DisplayDateTime(DateTime.Now, Profile);


Xml doc which holds time zones is located in app_data and looks like this:


version="1.0" encoding="utf-8" ?>
<option value="-12.00" 

>Eniwetok, Kwajalein</option>
<option value="-11.00" 

>Midway Island, Samoa</option>
<option value="-10.00" 

<option value="-9.00" >Alaska</option>
<option value="-8.00" >Pacific Time 
(US &amp; 
<option value="-7.00" >Mountain Time 
(US &amp; 
<option value="-6.00" >Central Time 
(US &amp; Canada), 
Mexico City</option>
<option value="-5.00" >Eastern Time 
(US &amp; Canada), 
Bogota, Lima, Quito</option>
<option value="-4.00" >Atlantic Time 
(Canada), Caracas, La Paz</option>
<option value="-3.50" >Newfoundland</option>
<option value="-3.00" >Brazil, 
Buenos Aires, Georgetown</option>
<option value="-2.00" >Mid-Atlantic</option>
<option value="-1.00" >Azores, Cape 
Verde Islands</option>
<option value="0.00" >Western 
Europe Time, London, Lisbon, Casablanca, Monrovia</option>
<option value="1.00" >CET(Central 
Europe Time), Brussels, Copenhagen, Madrid, Paris</option>
<option value="2.00" >EET(Eastern 
Europe Time), Kaliningrad, South Africa</option>
<option value="3.00" >Baghdad, 
Kuwait, Riyadh, Moscow, St. Petersburg, Volgograd, Nairobi</option>
<option value="3.50" >Tehran</option>
<option value="4.00" >Abu Dhabi, 
Muscat, Baku, Tbilisi</option>
<option value="4.50" >Kabul</option>
<option value="5.00" >Ekaterinburg, 
Islamabad, Karachi, Tashkent</option>
<option value="5.50" >Bombay, 
Calcutta, Madras, New Delhi</option>
<option value="6.00" >Almaty, 
Dhaka, Colombo</option>
<option value="7.00" >Bangkok, 
Hanoi, Jakarta</option>
<option value="8.00" >Beijing, 
Perth, Singapore, Hong Kong, Chongqing, Urumqi, Taipei</option>
<option value="9.00" >Tokyo, Seoul, 
Osaka, Sapporo, Yakutsk</option>
<option value="9.50" >Adelaide, 
<option value="10.00" >EAST(East 
Australian Standard), Guam, Papua New Guinea, Vladivostok</option>
<option value="11.00" >Magadan, 
Solomon Islands, New Caledonia</option>
<option value="12.00" >Auckland, 
Wellington, Fiji, Kamchatka, Marshall Island</option>
<option value="13.00" >Nuku&quot;alofa</option>

We are using XPathDocument class here. XPathDocument works fasrer than XmlDocument so we are using it to get time zone name based on the time difference.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.Remoting.Messaging;
using System.Xml.XPath;
using System.Xml;
/// <summary>
/// Summary description for 
/// </summary>
public class TimeZoneManager
/// Gets 
the time zone.
<param name="offset">The offset.</param>
private static string GetTimeZone(double offset)
//Adjust result based on the server time 
string timeZoneKey = 
"timeOffset" + 
string result = 
(CallContext.GetData(timeZoneKey) ?? "").ToString();
if (result.Length == 
string zonesDocPath = 
HttpRuntime.AppDomainAppPath +"app_data/TimeZones.xml";
timeZonesDoc = new 
string xPath = 
"/root/option[@value='" + 
offset.ToString("F") + 
searchNavigator = timeZonesDoc.CreateNavigator();
nsmgr = new XmlNamespaceManager(searchNavigator.NameTable);
iterator = searchNavigator.Select(xPath, nsmgr);
result = iterator.Current.Value;
if (!string.IsNullOrEmpty(result))
CallContext.SetData(timeZoneKey, result);
catch (Exception ex)
return ex.Message;
return result;
Displays the date time.
<param name="dateTime">The date time.</param>
<param name="profile">The profile.</param>
public static string DisplayDateTime(DateTime dateTime, ProfileCommon profile)
//Move to universal time (GMT) with Zero 
DateTime utcDateTime = 
//Add client side offset
DateTime resultDateTime 
= utcDateTime.AddHours(profile.hoursDiffStdTime + 1);
return string.Concat(resultDateTime.ToShortDateString(), ", ", resultDateTime.ToShortTimeString(), 
", ", 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralVB Code Problem Pin
imatureStudent12-Mar-10 5:43
memberimatureStudent12-Mar-10 5:43 
GeneralRe: VB Code Problem Pin
dshalimov18-Mar-10 13:12
memberdshalimov18-Mar-10 13:12 
AnswerRe: VB Code Problem Pin
thatraja28-Jan-12 6:00
protectorthatraja28-Jan-12 6:00 
GeneralJust What the Doctor Ordered Pin
Sirius B13-Oct-08 22:00
memberSirius B13-Oct-08 22:00 
Generalusername and password Pin
shahidriazonline1-Dec-07 10:35
membershahidriazonline1-Dec-07 10:35 
GeneralRe: username and password Pin
NazishTauheed6-Apr-08 15:16
memberNazishTauheed6-Apr-08 15:16 
Questionjust curious... Pin
jwaldner19-Jul-07 4:15
memberjwaldner19-Jul-07 4:15 
AnswerRe: just curious... Pin
Kirby Zhang28-Nov-07 14:09
memberKirby Zhang28-Nov-07 14:09 
GeneralDaylight saving time Pin
Christian Salvaneschi25-Sep-06 11:22
memberChristian Salvaneschi25-Sep-06 11:22 
GeneralRe: Daylight saving time Pin
Christian Salvaneschi26-Sep-06 12:48
memberChristian Salvaneschi26-Sep-06 12:48 
GeneralRe: Daylight saving time Pin
dshalimov27-Sep-06 15:09
memberdshalimov27-Sep-06 15:09 
GeneralRe: Daylight saving time [modified] Pin
Christian Salvaneschi28-Sep-06 13:23
memberChristian Salvaneschi28-Sep-06 13:23 
GeneralRe: Daylight saving time Pin
Kirby Zhang28-Nov-07 14:07
memberKirby Zhang28-Nov-07 14:07 
GeneralGood Pin
King_kLAx9-Sep-06 22:51
memberKing_kLAx9-Sep-06 22:51 
GeneralRe: Good Pin
King_kLAx15-Sep-06 0:03
memberKing_kLAx15-Sep-06 0:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190419.4 | Last Updated 8 Sep 2006
Article Copyright 2006 by dshalimov
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid