.NET 中忽略 DST(夏令时)
我试图将字符串转换为日期时间对象,而不对其进行修改并向日期时间对象添加额外的小时/偏移量。
这是代码:
string dateStr = "2011-03-18T12:07:00.000+10:00"; //Convert this string to datetime AS IS
DateTime date = Convert.ToDateTime(dateStr);
Console.WriteLine("Original string: " + dateStr);
Console.WriteLine("date toString: " + date.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffK"));
输出:
Original string: 2011-03-18T12:07:00.000+10:00
date toString: 2011-03-18T13:07:00.000+11:00
如果有人能指出我如何忽略 DST 或者只是将日期转换回我想要的 Datetime 对象,那就是非常感谢。
修改日期时间的路径: 我尝试将 Datetime 转换回来,但我无法弄清楚如何更改 Datetime 对象中的时区,并且当我使用 DatetimeOffset 对象来完成这项工作时,当我调用 DatetimeOffset.Datetime 时,它会返回没有偏移量的日期时间
I am trying to convert a String to a Datetime object without it modifying it and adding an extra hour/offset to the Datetime object.
This is the code:
string dateStr = "2011-03-18T12:07:00.000+10:00"; //Convert this string to datetime AS IS
DateTime date = Convert.ToDateTime(dateStr);
Console.WriteLine("Original string: " + dateStr);
Console.WriteLine("date toString: " + date.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffK"));
The output:
Original string: 2011-03-18T12:07:00.000+10:00
date toString: 2011-03-18T13:07:00.000+11:00
If anyone can point how I can ignore DST or simply even convert the date back to what I want as a Datetime object, that would be much appreciated.
Going down the path of modifying the datetime:
I have tried to convert the Datetime back but I cannot work out how to change the timezone in the Datetime object and when I use a DatetimeOffset object to do this work, when I call the DatetimeOffset.Datetime it returns the Datetime without the offset
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DateTime
对象不包含时区。它仅包含一个Kind
,可以是Utc
、Local
或Unspecified
。调用
Convert.ToDateTime
时,如果存在任何偏移量,则将应用该偏移量,然后将该值转换为本地时区。即 - 代码运行的时区。然后,通过在输出格式中使用
K
,它会返回该特定日期的本地时区的偏移量。要正确处理这种情况,请使用
DateTimeOffset
类型:关于您的语句:
这是正确的。只有
DateTimeOffset
类型有要返回的偏移量。DateTime
对象只能返回机器的本地偏移量,或者对于具有DateTimeKind.Utc
的DateTime
为零。A
DateTime
object doesn't contain a time zone. It only contains aKind
, which can beUtc
,Local
, orUnspecified
.When calling
Convert.ToDateTime
, if any offset is present, that offset will be applied and then the value will be converted to the local time zone. That is - the time zone where the code is running.Then, by using
K
in the output format, it returns the offset of the local time zone for that particular date.To handle this scenario correctly, use the
DateTimeOffset
type:Regarding your statement:
That is correct. Only the
DateTimeOffset
type has an offset to return. TheDateTime
object can only return the local offset of the machine, or zero for aDateTime
withDateTimeKind.Utc
..NET框架中的TimezoneInfo类可以提供答案(http://msdn. microsoft.com/en-us/library/system.timezoneinfo.aspx)
TimezoneInfo class in .NET framework can provide the answer (http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx)
DateTime dt = DateTime.Now.ToUniversalTime() + TimeZoneInfo.Local.BaseUtcOffset;
DateTime dt = DateTime.Now.ToUniversalTime() + TimeZoneInfo.Local.BaseUtcOffset;