5,702,067 members and growing! (14,852 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

DateTime Values according to the remote time zone

By dshalimov

Shows how to define timezone of the remote user and display date time values according to remote time zonoe
Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 8 Sep 2006
Updated: 8 Sep 2006
Views: 33,693
Bookmarked: 31 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 3.06 Rating: 3.94 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 16.7%
3
3 votes, 50.0%
4
2 votes, 33.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

 

Introduction

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

 

web.config:

 

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

....

<profile enabled="true" >

<properties>

<add name="hoursDiffStdTime" defaultValue="0" type="double"/>

</properties>

</profile>

....

login.aspx:

 

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

.....

<head runat="server">

<title>Welcome</title>

<script type="text/javascript">

function GetLocalTimeOffset()

{

var rightNow = new Date();

var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);

var temp = date1.toGMTString();

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

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

createCookie("hoursDiffStdTime", hoursDiffStdTime, 0);

}

function createCookie(name,value,days) {

if (days && days > 0) {

var date = new Date();

date.setTime(date.getTime()+(days*24*60*60*1000));

var expires = "; expires="+date.toGMTString();

}

else var expires = "";

document.cookie = name+"="+value+expires+"; path=/";

}

</script>

</head>

<body onload="GetLocalTimeOffset();">

...

default.aspx:  

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);

timezonemanager.cs:

 

 

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

 

<?xml version="1.0" encoding="utf-8" ?>

<root>

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

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

<option value="-10.00" >Hawaii</option>

<option value="-9.00" >Alaska</option>

<option value="-8.00" >Pacific Time (US &amp; Canada)</option>

<option value="-7.00" >Mountain Time (US &amp; Canada)</option>

<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, Darwin</option>

<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>

</root> 

 

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 TimeZoneManager

/// </summary>

public class TimeZoneManager

{

/// <summary>

/// Gets the time zone.

/// </summary>

/// <param name="offset">The offset.</param>

/// <returns></returns>

private static string GetTimeZone(double offset)

{

//Adjust result based on the server time zone

string timeZoneKey = "timeOffset" + offset.ToString("F");

string result = (CallContext.GetData(timeZoneKey) ?? "").ToString();

if (result.Length == 0)

{

string zonesDocPath = HttpRuntime.AppDomainAppPath +"app_data/TimeZones.xml";

XPathDocument timeZonesDoc = new XPathDocument(zonesDocPath);

try

{

string xPath = "/root/option[@value='" + offset.ToString("F") + "']";

XPathNavigator searchNavigator = timeZonesDoc.CreateNavigator();

XmlNamespaceManager nsmgr = new XmlNamespaceManager(searchNavigator.NameTable);

XPathNodeIterator iterator = searchNavigator.Select(xPath, nsmgr);

if (iterator.MoveNext())

{

result = iterator.Current.Value;

if (!string.IsNullOrEmpty(result))

{

CallContext.SetData(timeZoneKey, result);

}

}

}

catch (Exception ex)

{

return ex.Message;

}

}

return result;

}

/// <summary>

/// Displays the date time.

/// </summary>

/// <param name="dateTime">The date time.</param>

/// <param name="profile">The profile.</param>

/// <returns></returns>

public static string DisplayDateTime(DateTime dateTime, ProfileCommon profile)

{

//Move to universal time (GMT) with Zero offset

DateTime utcDateTime = dateTime.ToUniversalTime();

//Add client side offset

DateTime resultDateTime = utcDateTime.AddHours(profile.hoursDiffStdTime + 1);

return string.Concat(resultDateTime.ToShortDateString(), ", ", resultDateTime.ToShortTimeString(), ", ", GetTimeZone(profile.hoursDiffStdTime));

}

}

 

 

 

 

 

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

dshalimov



Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
GeneralJust What the Doctor OrderedmemberSirius B23:00 13 Oct '08  
GeneralGood Link for Understanding DateTimememberDotNetGuts9:04 18 Jun '08  
Generalusername and passwordmembershahidriazonline11:35 1 Dec '07  
GeneralRe: username and passwordmemberNazishTauheed16:16 6 Apr '08  
Questionjust curious...memberjwaldner5:15 19 Jul '07  
AnswerRe: just curious...memberKirby Zhang15:09 28 Nov '07  
GeneralDaylight saving timememberChristian Salvaneschi12:22 25 Sep '06  
GeneralRe: Daylight saving timememberdshalimov10:23 26 Sep '06  
GeneralRe: Daylight saving timememberChristian Salvaneschi13:48 26 Sep '06  
GeneralRe: Daylight saving timememberdshalimov16:09 27 Sep '06  
GeneralRe: Daylight saving time [modified]memberChristian Salvaneschi14:23 28 Sep '06  
GeneralRe: Daylight saving timememberKirby Zhang15:07 28 Nov '07  
GeneralGoodmemberKing_kLAx23:51 9 Sep '06  
GeneralRe: Goodmemberdshalimov4:24 11 Sep '06  
GeneralRe: GoodmemberKing_kLAx1:03 15 Sep '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Sep 2006
Editor:
Copyright 2006 by dshalimov
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project