tkl33
tkl33

Reputation: 313

UTC offset in minutes

How can I get the difference between local time and UTC time in minutes (in C#)?

Upvotes: 13

Views: 12625

Answers (6)

Pete Stensønes
Pete Stensønes

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

Kevin Junghans
Kevin Junghans

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

Cristian Lupascu
Cristian Lupascu

Reputation: 40556

Yet another version:

DateTimeOffset.Now.Offset.TotalMinutes

Upvotes: 6

Response.Write((DateTime.Now - DateTime.UtcNow).TotalMinutes);

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160922

Use TimeZoneInfo:

TimeSpan delta = TimeZoneInfo.Local.GetUtcOffset();
double utcMinuteOffset = delta.TotalMinutes;

Upvotes: 24

Adam S
Adam S

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

Related Questions