Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Posts Tagged ‘timezoneinfo’

Importance of TimeZoneInfo class

Posted by Paul on February 3, 2011

I would like to take your attention on the following problems.

You are working on an ASP.NET application which is hosted in a server under USA time zone?

You need to send an email to your customers daily at Singapore time zone 10 AM. How is that possible?

How to get all the time zones in the current system?

What is the current time zone Id?

The answers for all these questions would be the TimeZoneInfo class.

TimeZoneInfo

The TimeZoneInfo class represents any time zone in the world. It contains a set of static methods/properties that allows us to get the current time zone information, conversion of datetime between time zones etc.

clip_image002

The important properties or methods are described below:

Name

Type

Description

Local

Static Property

Gets the current TimeZoneInfo of machine

GetSystemTimeZones()

Static Method

Returns the list of world TimeZoneInfo available

ConvertTime()

Static Method

Converts time in one zone to another

Id

Instance Property

String identifier of TimeZoneInfo

DisplayName

Instance Property

Display name in localized text

GetUtcOffset()

Instance Method

Returns the difference in timezone difference with Universal Time (UTC)

The Test Application

The test application attached provides:

· Information on Current Time Zone

· Displays list of TimeZoneInfo available

clip_image004

Converting current datetime to another TimeZone

So what about converting the current time to another time zone?

Here is the answer: Use the TimeZoneInfo.ConvertTime() method.

Code Example:

TimeZoneInfo timeZone = TimeZoneInfo.GetSystemTimeZones().Where(t => t.Id == “Central Standard Time”).First();

DateTime newTime = TimeZoneInfo.ConvertTime(DateTime.Now, timeZone);

The method ConvertTime() takes the current time and new time zone as arguments.

Advertisements

Posted in C# | Tagged: , | Leave a Comment »