Home > DeveloperSection > Blogs > TimeZone Class in C#

TimeZone Class in C#


C# C# 
Ratings:
0 Comment(s)
 1710  View(s)
Rate this:

TimeZone Class in C#

In this blog I am trying to explain the concept of TimeZone Class in C#.

TimeZone

A time zone is a geographical region in which the same standard time is used. You can use the TimeZone class to get information about the current time zone, and to convert times from local time to Coordinated Universal Time (UTC) or vice versa. However, you cannot use the TimeZone class to represent time zones other than the local zone or to handle date and time conversions from one time zone to another. For this purpose, use the TimeZoneInfo class. You can use this class to retrieve information on any time zone defined on the local system, to create custom time zones, and to convert times from one time zone to another.

Syntax: -

[SerializableAttribute]

[ComVisibleAttribute(true)]

public abstract class TimeZone

TimeZone Example: -

You need the System.Globalization namespace for use the time zone classes.

1.  class TimeZoneDemo

2.  {

3.  static void Main()

4.  {

5.  Console.WriteLine("{0,-30}{1}", "Standard time name:",

   TimeZone.CurrentTimeZone.StandardName);

6.  Console.WriteLine("{0,-30}{1}", "Daylight saving time name:",

    TimeZone.CurrentTimeZone.DaylightName);

7.  Console.WriteLine("\n" + "{0,-30}{1:MM-dd-yyyy HH:mm}", "Current date and time:",

   DateTime.Now);

8.  Console.WriteLine("{0,-30}{1}", "Daylight saving time?",

   TimeZone.CurrentTimeZone.IsDaylightSavingTime(DateTime.Now));

9.  DateTime currentUTC =TimeZone.CurrentTimeZone.ToUniversalTime(DateTime.Now);

10. TimeSpan currentOffset =TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);

11. Console.WriteLine("{0,-30}{1:MM-dd-yyyy HH:mm}", "Coordinated Universal Time:",

   currentUTC);

12. Console.WriteLine("{0,-30}{1}", "UTC offset:", currentOffset);

13. DaylightTime daylight =

   TimeZone.CurrentTimeZone.GetDaylightChanges(DateTime.Now.Year);

14. Console.WriteLine("\nDaylight saving time for year {0}:", DateTime.Now.Year);

15. Console.WriteLine("{0:MM-dd-yyyy HH:mm} to " +"{1:MM-dd-yyyy HH:mm}, delta: {2}",

   daylight.Start, daylight.End, daylight.Delta);

16. Console.ReadKey();

17. }

18. }

Code description: -

1.       Create a TimeZone class.

2.       Start Main method.

5.       Display the names for standard time and daylight saving.

6.       Time for the local time zone.

7.       Display the current date and time and show if they occur.

8.       Display the current date and time in daylight saving time.

9.       Get the current Coordinated Universal Time (UTC) and UTC.

10.   Get the current Coordinated Universal Time (UTC) and UTC offset.

11.   Show the current Coordinated Universal Time.

12.   Show the Coordinated Universal Time offset.

13.   Get the DaylightTime object for the current year.

14.   Display the daylight saving time range for the current year.

Output: -


Don't want to miss updates? Please click the below button!

Follow MindStick