C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
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
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
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
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
Then: We can call methods that reveal information about daylight saving time universal times and UTC offsets.
DateTime