C# 检查夏令时是否生效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10665678/
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
Check if daylight savings is in effect?
提问by Megaoctane
How to check if in Denmark daylight time savings has taken effect, if so, then add 1 hour to my data, else not? I have a xml file:
如何检查丹麦的夏令时是否已生效,如果是,则在我的数据中添加 1 小时,否则不会?我有一个 xml 文件:
<day = "1"
month = "5"
sunrise ="06:30"
sunset ="21:30"
/>
采纳答案by Eugene
Think you need convert this xml to DateTime and then use TimeZoneInfo class.
认为您需要将此 xml 转换为 DateTime,然后使用 TimeZoneInfo 类。
If Denmark your local time:
如果丹麦是您的当地时间:
DateTime thisTime = DateTime.Now;
bool isDaylight = TimeZoneInfo.Local.IsDaylightSavingTime(thisTime);
Else you need to get Denmark TimeZone:
否则您需要获取丹麦时区:
DateTime thisTime = DateTime.Now;
// get Denmark Standard Time zone - not sure about that
TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("Denmark Standard Time");
bool isDaylight = tst.IsDaylightSavingTime(thisTime);
回答by Matthew
You can use TimeZoneInfo.IsDaylightSavingTime
您可以使用TimeZoneInfo.IsDaylightSavingTime
DateTime theDate = new DateTime(2012, 5, 1); // may 1st
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
bool isCurrentlyDaylightSavings = tzi.IsDaylightSavingTime(theDate);
回答by Meir Schreiber
When I coded as above - for New-York, I found in the debugger that the time was set correctly (including DST)
当我如上编码时 - 对于纽约,我在调试器中发现时间设置正确(包括 DST)
TimeZoneInfo nyTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime nyTime = GetLocalDateTime(DateTime.UtcNow, nyTimeZone);
if (nyTimeZone.IsDaylightSavingTime(nyTime))
nyTime = nyTime.AddHours(1);
public static DateTime GetLocalDateTime(DateTime utcDateTime, TimeZoneInfo timeZone)
{
utcDateTime = DateTime.SpecifyKind(utcDateTime, DateTimeKind.Utc);
DateTime time = TimeZoneInfo.ConvertTime(utcDateTime, timeZone);
return time;
}
回答by timv
Here is a generic test and happy to be corrected if my math is incorrect. In my case I just needed to get the GMT offset for the timezone regardless of where it was in the world.
这是一个通用测试,如果我的数学不正确,很高兴得到纠正。在我的情况下,我只需要获取时区的 GMT 偏移量,而不管它在世界上的哪个位置。
int timezone;
TimeZoneInfo localZone = TimeZoneInfo.Local;
DateTime myTime = DateTime.Now;
bool isDayLight = TimeZoneInfo.Local.IsDaylightSavingTime(myTime);
if (isDayLight)
timezone = Math.Abs(localZone.BaseUtcOffset.Hours) + 1;
else
timezone = Math.Abs(localZone.BaseUtcOffset.Hours);
Debug.WriteLine("timezone is " + timezone);
I simply found the current time and if it was in Day Light Savings period added +1 to the GMT offset.
我只是找到了当前时间,如果是在夏令时,则在 GMT 偏移量上加上 +1。
This works with Visual Studio Express 2013.
这适用于 Visual Studio Express 2013。
回答by B.W
You need to do two things:
你需要做两件事:
- call
IsAmbiguous - List item
IsDaylightSavingTime.
- 称呼
IsAmbiguous - 列表项
IsDaylightSavingTime。
if (TimeZoneInfo.Local.IsAmbiguousTime(unclearDate) ||
TimeZoneInfo.Local.IsDaylightSavingTime(unclearDate))
Console.WriteLine("{0} may be daylight saving time in {1}.", unclearDate, TimeZoneInfo.Local.DisplayName);
if (TimeZoneInfo.Local.IsAmbiguousTime(unclearDate) ||
TimeZoneInfo.Local.IsDaylightSavingTime(unclearDate))
Console.WriteLine("{0} may be daylight saving time in {1}.", unclearDate, TimeZoneInfo.Local.DisplayName);
https://msdn.microsoft.com/en-us/library/bb460642(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/bb460642(v=vs.110).aspx
回答by Marc
this is my short solution which can be using in all timezones:
这是我可以在所有时区使用的简短解决方案:
DateTime utcTime = DateTime.Parse("30.10.2018 18:21:34")
DateTime localtime = ConvertUTCToLocalTime(utcTime);
public static DateTime ConvertUTCToLocalTime(DateTime UTCTime)
{
var localZone = TimeZone.CurrentTimeZone;
var offset = localZone.GetUtcOffset(UTCTime);
var localTime = UTCTime.AddHours(offset.Hours);
return localTime;
}
回答by HellFyr
Important
重要的
myDateTime.IsDaylightSavingTime DOES return the proper value... but... it is accurate down to at least the hour of the day, just passing the date alone isn't enough.
myDateTime.IsDaylightSavingTime 确实返回正确的值......但是......它至少精确到一天中的小时,仅传递日期是不够的。
For instance this year (2019) 3/10/2019 02:00:00 passed as myDateTime will return false, but 3/10/2019 03:00:00 will return true.
例如,今年 (2019) 3/10/2019 02:00:00 作为 myDateTime 传递将返回 false,但 3/10/2019 03:00:00 将返回 true。

