UTC offset in minutes

2020-08-12 18:39发布

问题:

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

回答1:

Use TimeZoneInfo:

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


回答2:

This should give you what you need.

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

Also you may find the .ToUniversalTime DateTime function of use.



回答3:

Yet another version:

DateTimeOffset.Now.Offset.TotalMinutes


回答4:

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


回答5:

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());


回答6:

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);


标签: c# .net