Reputation: 313
How can I get the difference between local time and UTC time in minutes (in C#)?
Upvotes: 13
Views: 12625
Reputation: 5665
See this MSDN artice for the full details. The code sample at the end of the article expressly gives code to get the difference between local and UTC time.
For those that don't want to click the link here is an extract from that code:
// Find difference between Date.Now and Date.UtcNow
date1 = DateTime.Now;
date2 = DateTime.UtcNow;
difference = date1 - date2;
Console.WriteLine("{0} - {1} = {2}", date1, date2, difference);
Upvotes: 0
Reputation: 17540
DateTime localDt = DateTime.Now;
DateTime utcDt = DateTime.UtcNow;
TimeSpan localUtcDiff = utcDt.Subtract(localDt);
Console.WriteLine("The difference in minutes between local time and UTC time is " + localUtcDiff.TotalMinutes.ToString());
Upvotes: 0
Reputation: 40556
Yet another version:
DateTimeOffset.Now.Offset.TotalMinutes
Upvotes: 6
Reputation: 68526
Response.Write((DateTime.Now - DateTime.UtcNow).TotalMinutes);
Upvotes: 1
Reputation: 160922
Use TimeZoneInfo
:
TimeSpan delta = TimeZoneInfo.Local.GetUtcOffset();
double utcMinuteOffset = delta.TotalMinutes;
Upvotes: 24
Reputation: 3125
This should give you what you need.
(DateTime.UtcNow - DateTime.Now).TotalMinutes;
Also you may find the .ToUniversalTime
DateTime function of use.
Upvotes: 7