Click here to Skip to main content
5,786,882 members and growing! (19,998 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Beginner License: The GNU Lesser General Public License

Easily Work With Differing Time Zones In An ASP.NET 3.5 Application

By BrantBurnett

Class library to assist in using different time zones in the same ASP.NET 3.5 application
C# (C# 3.0, C#), VB (VB 9.0, VB), .NET (.NET, .NET 3.5), IIS, ASP.NET, WebForms, Architect, Dev

Posted: 18 Jun 2008
Updated: 18 Jun 2008
Views: 4,890
Bookmarked: 17 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 2.68 Rating: 3.17 out of 5
2 votes, 28.6%
1
0 votes, 0.0%
2
2 votes, 28.6%
3
1 vote, 14.3%
4
2 votes, 28.6%
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

When working with ASP.NET applications, time zones are often a problem when dealing with DateTime structures. There are two different common scenarios that a developer is likely to encounter. The first is that you are placing the application on a hosted server that is in a different time zone than the business you are trying to operate. The second is that you have users from different time zones and would like to display the date and time to the user in their local time zone rather than the server's time zone.

Hosted Time Zone addresses this problem by providing two settings. ApplicationTimeZone sets the time zone to be used by the entire application. ThreadTimeZone sets the time zone to be used by the currently running thread, which defaults to the ApplicationTimeZone if no time zone has been specified on the thread. If you are using ThreadTimeZone then you need to be sure to set the value for each request made to the server. You will usually do this in your global.asax file, such as in the Application_BeginRequest handler.

Using the code

To actually perform the conversions you can call the ToAppTime, FromAppTime, ToThreadTime, or FromThreadTime static methods on the ApplicationTimeZone class. Additionally, extension methods have been declared for the DateTime and DateTimeOffset classes that perform the same functions more cleanly. In order for the extension methods to work you must import the HostedTimeZone namespace.

You should note that there are not any overloads for FromAppTime or FromThreadTime for the DateTimeOffset class. This is because the ToLocalTime method of DateTimeOffset performs the same function, so they would be redundant.

Points of Interest

This library requires the TimeZoneInfo class, which is not available until the .NET Framework 3.5. Therefore, it is not compatible with ASP.NET 2.0 or earlier.

History

1.0.0.0 6/18/2008 Initial Release

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License

About the Author

BrantBurnett



Occupation: Software Developer (Senior)
Company: Pathfinder Software
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 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
General2.0 version?memberRonald Bastien15:36 27 Oct '08  
AnswerRe: 2.0 version?memberBrantBurnett18:43 28 Oct '08  
AnswerRe: 2.0 version?memberBrantBurnett5:06 4 Nov '08  
GeneralInterestingmembermerlin9819:24 20 Jun '08  

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

PermaLink | Privacy | Terms of Use
Last Updated: 18 Jun 2008
Editor:
Copyright 2008 by BrantBurnett
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project