Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Get Current Time Zone In C#
WhatsApp
Mahesh Chand
5y
256.1k
0
9
100
Article
This code snippet demonstrates how to get time difference between GMT and local time zone using TimeZone class in .NET.
The following code uses the TimeZone class to get the name of the current time zone, difference between GMT and local time zones, and also the details about day light saving time.
The TimeZone.CurrentTimeZone returns the current timezone of the current machine.
const
string
dataFmt =
"{0,-30}{1}"
;
const
string
timeFmt =
"{0,-30}{1:MM-dd-yyyy HH:mm}"
;
TimeZone curTimeZone = TimeZone.CurrentTimeZone;
// What is TimeZone name?
Console.WriteLine( dataFmt,
"TimeZone Name:"
, curTimeZone.StandardName );
// Is TimeZone DayLight Saving?|
Console.WriteLine( dataFmt,
"Daylight saving time?"
, curTimeZone.IsDaylightSavingTime( DateTime.Now ) );
// What is GMT (also called Coordinated Universal Time (UTC)
DateTime curUTC = curTimeZone.ToUniversalTime( DateTime.Now );
Console.WriteLine( timeFmt,
"Coordinated Universal Time:"
, curUTC );
// What is GMT/UTC offset ?
TimeSpan currentOffset = curTimeZone.GetUtcOffset( DateTime.Now );
Console.WriteLine( dataFmt,
"UTC offset:"
, currentOffset );
// Get DaylightTime object
System.Globalization.DaylightTime dl = curTimeZone.GetDaylightChanges( DateTime.Now.Year );
// DateTime when the daylight-saving period begins.
Console.WriteLine(
"Start: {0:MM-dd-yyyy HH:mm} "
, dl.Start);
// DateTime when the daylight-saving period ends.
Console.WriteLine(
"End: {0:MM-dd-yyyy HH:mm} "
, dl.End);
// Difference between standard time and the daylight-saving time.
Console.WriteLine(
"delta: {0}"
, dl.Delta );
Console.Read();
Current time zone
Time Zone class
TimeZone
Universal time
Up Next
Ebook Download
View all
Printing in C# Made Easy
Read by 22.3k people
Download Now!
Learn
View all
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.
Membership not found