.NET 中忽略 DST(夏令时)

发布于 2024-11-30 17:21:32 字数 774 浏览 2 评论 0原文

我试图将字符串转换为日期时间对象,而不对其进行修改并向日期时间对象添加额外的小时/偏移量。

这是代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听,心雨的声音 2024-12-07 17:21:32

DateTime 对象不包含时区。它仅包含一个 Kind,可以是 UtcLocalUnspecified

调用 Convert.ToDateTime 时,如果存在任何偏移量,则将应用该偏移量,然后将该值转换为本地时区。即 - 代码运行的时区。

然后,通过在输出格式中使用 K,它会返回该特定日期的本地时区的偏移量。

要正确处理这种情况,请使用 DateTimeOffset 类型:

string dateStr = "2011-03-18T12:07:00.000+10:00";

DateTimeOffset dto = DateTimeOffset.Parse(dateStr);

Console.WriteLine("Original string: " + dateStr);
Console.WriteLine("dto toString:    " + dto.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffK"));

关于您的语句:

当我调用 DatetimeOffset.Datetime 时,它​​返回没有偏移量的日期时间

这是正确的。只有 DateTimeOffset 类型有要返回的偏移量。 DateTime 对象只能返回机器的本地偏移量,或者对于具有 DateTimeKind.UtcDateTime 为零。

A DateTime object doesn't contain a time zone. It only contains a Kind, which can be Utc, Local, or Unspecified.

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:

string dateStr = "2011-03-18T12:07:00.000+10:00";

DateTimeOffset dto = DateTimeOffset.Parse(dateStr);

Console.WriteLine("Original string: " + dateStr);
Console.WriteLine("dto toString:    " + dto.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffK"));

Regarding your statement:

when I call the DatetimeOffset.Datetime it returns the Datetime without the offset

That is correct. Only the DateTimeOffset type has an offset to return. The DateTime object can only return the local offset of the machine, or zero for a DateTime with DateTimeKind.Utc.

煞人兵器 2024-12-07 17:21:32

.NET框架中的TimezoneInfo类可以提供答案(http://msdn. microsoft.com/en-us/library/system.timezoneinfo.aspx

string dateStr = "2011-03-18T12:07:00.000+10:00"; //Convert this string to datetime AS IS

DateTime localDateTime = DateTime.Parse(date); 

DateTime utcDateTime = localDateTime.ToUniversalTime();
string estKey = "Eastern Standard Time";
TimeZoneInfo estTimeZone = TimeZoneInfo.FindSystemTimeZoneById(estKey);
DateTime estDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, estTimeZone);

Console.WriteLine("Original string: " + dateStr);
Console.WriteLine("date toString:   " + estDateTime);

TimezoneInfo class in .NET framework can provide the answer (http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx)

string dateStr = "2011-03-18T12:07:00.000+10:00"; //Convert this string to datetime AS IS

DateTime localDateTime = DateTime.Parse(date); 

DateTime utcDateTime = localDateTime.ToUniversalTime();
string estKey = "Eastern Standard Time";
TimeZoneInfo estTimeZone = TimeZoneInfo.FindSystemTimeZoneById(estKey);
DateTime estDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, estTimeZone);

Console.WriteLine("Original string: " + dateStr);
Console.WriteLine("date toString:   " + estDateTime);
春夜浅 2024-12-07 17:21:32

DateTime dt = DateTime.Now.ToUniversalTime() + TimeZoneInfo.Local.BaseUtcOffset;

DateTime dt = DateTime.Now.ToUniversalTime() + TimeZoneInfo.Local.BaseUtcOffset;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文