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