TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# TimeZone Examples

Use the TimeZone type, which is a class in the System namespace that represents different time zones.
TimeZone describes the current time zone. It gets information about the time zone of the current computer. Time zones on the planet Earth change on lines on longitude. You cannot assume a particular time zone.
Names. What is the name of a specific time zone? The .NET Framework contains information about the names for time zones. You can access the properties StandardName and DaylightName to get these strings.

Also: This example shows how to get the current time zone with the TimeZone.CurrentTimeZone property accessor.

C# program that gets time zone names using System; class Program { static void Main() { // Get current time zone. TimeZone zone = TimeZone.CurrentTimeZone; string standard = zone.StandardName; string daylight = zone.DaylightName; Console.WriteLine(standard); Console.WriteLine(daylight); } } Output Mountain Standard Time Mountain Daylight Time
Universal time. There are two concepts of time you can use. The local time depends on where the computer is located. The universal time is independent of this. With universal times, you can compare times from different locations with no errors.
C# program that checks universal and local times using System; class Program { static void Main() { TimeZone zone = TimeZone.CurrentTimeZone; // Demonstrate ToLocalTime and ToUniversalTime. DateTime local = zone.ToLocalTime(DateTime.Now); DateTime universal = zone.ToUniversalTime(DateTime.Now); Console.WriteLine(local); Console.WriteLine(universal); } } Output 8/26/2010 1:02:28 PM 8/26/2010 7:02:28 PM
Get offsets. A UTC offset indicates how many hours a time zone differs from the Coordinated Universal Time. When you call GetUtcOffset on a TimeZone instance, you get a TimeSpan instance that indicates how many hours the time is different from UTC.TimeSpan

Note: Coordinated Universal Time is also referred to as Zulu Time or Greenwich Mean Time.

C# program that checks UTC offset using System; class Program { static void Main() { TimeZone zone = TimeZone.CurrentTimeZone; // Get offset. TimeSpan offset = zone.GetUtcOffset(DateTime.Now); Console.WriteLine(offset); } } Output -06:00:00
Day light changes. In this example, we get information about the current year's daylight saving changes. If you need to know when the Day light Changes are, this method is ideal.
C# program that shows daylight changes using System; using System.Globalization; class Program { static void Main() { // Get daylight saving information for current time zone. TimeZone zone = TimeZone.CurrentTimeZone; DaylightTime time = zone.GetDaylightChanges(DateTime.Today.Year); Console.WriteLine("Start: {0}", time.Start); Console.WriteLine("End: {0}", time.End); Console.WriteLine("Delta: {0}", time.Delta); } } Output Start: 3/14/2010 2:00:00 AM End: 11/7/2010 2:00:00 AM Delta: 01:00:00
Daylight saving time. This program scans individual dates for their daylight saving status. For repeated usage, it is probably better to use GetDaylightChanges instead of IsDaylightSavingTime. But IsDaylightSavingTime can be useful in some cases.
C# program that checks daylight saving time using System; class Program { static void Main() { DateTime d = new DateTime(2010, 1, 1); TimeZone cur = TimeZone.CurrentTimeZone; bool flag = !cur.IsDaylightSavingTime(d); for (int i = 0; i < 365; i++) { bool f = cur.IsDaylightSavingTime(d); if (f != flag) { Console.WriteLine("{0}: begin {1}", d, f); flag = f; } d = d.AddDays(1); } } } Output 1/1/2010 12:00:00 AM: begin False 3/15/2010 12:00:00 AM: begin True 11/8/2010 12:00:00 AM: begin False
Summary. The TimeZone type in the C# language and .NET Framework provides a handy way to get information about time zones. We use the CurrentTimeZone property to get the information for the computer.

Then: We can call methods that reveal information about daylight saving time universal times and UTC offsets.

DateTime
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf