在 C# 中获取服务器的时区偏移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17974633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:57:43  来源:igfitidea点击:

Get Timezone Offset of Server in C#

c#.net

提问by user1886419

How can I get the timezone offset of the physical server running my code? Not some date object or other object in memory.

如何获取运行我的代码的物理服务器的时区偏移量?不是内存中的某个日期对象或其他对象。

For example, the following code will output -4:00:00:

例如,以下代码将输出-4:00:00

<%= TimeZone.CurrentTimeZone.GetUtcOffset(new DateTime()) %>

When it should be -03:00:00because of daylight savings

什么时候应该是-03:00:00因为夏令时

采纳答案by Jon Skeet

new DateTime()will give you January 1st 0001, rather than the currentdate/time. I suspect you want the currentUTC offset... and that's why you're not seeing the daylight saving offset in your current code.

new DateTime()将为您提供 1 月 1 日 0001,而不是当前日期/时间。我怀疑您想要当前的UTC 偏移量……这就是为什么您在当前代码中看不到夏令时偏移量的原因。

I'd use TimeZoneInfo.Localinstead of TimeZone.CurrentTimeZone- it maynot affect things, but it would definitely be a better approach. TimeZoneInfoshould pretty much replace TimeZonein all code. Then you can use GetUtcOffset:

我会使用TimeZoneInfo.Local而不是TimeZone.CurrentTimeZone- 它可能不会影响事情,但这绝对是一个更好的方法。TimeZoneInfo应该几乎替换TimeZone所有代码。然后你可以使用GetUtcOffset

var offset = TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow);

(Using DateTime.Nowshouldwork as well, but it involves some magic behind the scenes when there are daylight saving transitions around now. DateTimeactually has fourkinds rather than the advertised three, but it's simpler just to avoid the issue entirely by using UtcNow.)

(使用DateTime.Now应该也可以,但是当现在有夏令时转换时,它在幕后会涉及一些魔法。DateTime实际上有四种而不是广告中的三种,但是通过使用完全避免这个问题更简单UtcNow。)

Or of course you could use my Noda Timelibrary instead of all this BCL rubbish ;) (If you're doing a lot of date/time work I'd thoroughly recommend that - obviously - but if you're onlydoing this one bit, it would probably be overkill.)

或者当然你可以使用我的Noda Time库而不是所有这些 BCL 垃圾;)(如果你正在做大量的日期/时间工作,我会彻底推荐 - 显然 - 但如果你只做一点点,这可能是矫枉过正。)

回答by p.s.w.g

There seems to be some difference between how GetUtcOffsetworks with new DateTime()and DateTime.Now. When I run it in the Central Time Zone, I get:

和 的GetUtcOffset工作方式之间似乎存在一些差异。当我在中央时区运行它时,我得到:new DateTime()DateTime.Now

TimeZone.CurrentTimeZone.GetUtcOffset(new DateTime()) // -06:00:00

TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)   // -05:00:00

It's a bit of a kludge, but I suppose you could also do this:

这有点麻烦,但我想你也可以这样做:

DateTime.Now - DateTime.UtcNow // -05:00:00

回答by asafrob

When you contract a new DateTimeobject it gets DateTime.MinValueWhen you get it's TimeZone.CurrentTimeZone.GetUtcOffsetyou actually get the time offset for that date. If you use the DateTime.Nowyou will get the current date & time and therefor the current offset.

当您签约一个新DateTime对象时,它会得到DateTime.MinValue当您得到它时,TimeZone.CurrentTimeZone.GetUtcOffset您实际上会得到该日期的时间偏移量。如果您使用 ,DateTime.Now您将获得当前日期和时间以及当前偏移量。

回答by Kasey Speakman

Since .NET 3.5, you can use the following from DateTimeOffsetto get the current offset.

从 .NET 3.5 开始,您可以使用以下 fromDateTimeOffset来获取当前偏移量。

var offset = DateTimeOffset.Now.Offset;

MSDN documentation

MSDN 文档