gpt4 book ai didi

c# - 日期时间解析错误 : The supplied DateTime represents an invalid time

转载 作者:太空狗 更新时间:2023-10-29 17:44:26 33 4
gpt4 key购买 nike

我有一种情况,日期是 "3/13/2016 2:41:00 AM"。当我按时区转换日期时,出现错误。

DateTime dt = DateTime.Parse("3/13/2016 2:41:00 AM");
DateTime Date_Time = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt, "Eastern Standard Time",
"GMT Standard Time");
Response.Write(dt);

执行后,我得到这个错误:

The supplied DateTime represents an invalid time. For example, when the clock is adjusted forward, any time in the period that is skipped is invalid. Parameter name: dateTime

最佳答案

尝试检查时间是否有歧义或有效时间。 Due to the daylight change您提到的时间,即 2:41:00 AM 不存在,因为时钟提前 1 小时移动,因此日期无效或不明确。

2016    Sun, 13 Mar, 02:00  CST → CDT   +1 hour (DST start) UTC-5h
Sun, 6 Nov, 02:00 CDT → CST -1 hour (DST end) UTC-6h

也可以引用这篇博客:System.TimeZoneInfo: Working with Ambiguous and Invalid Points in Time

System.TimeZoneInfo (currently available as part of .NET Framework 3.5 Beta 1) contains methods for checking if a DateTime instance represents an ambiguous or invalid time in a specific time zone. These methods are particularly useful for validating user-supplied points in time.

Background Information

Time zones that adjust their time for Daylight Saving Time (in most cases by moving the clock time back or forward by 1 hour) have gaps and repeats in the timeline — wherever the clock time was moved forward or back by the adjustment. Let’s use Pacific Standard Time as an example. In 2007 Pacific Standard Time (PST) changes to Pacific Daylight Time (PDT) at 02:00AM (“spring forward”) on the second Sunday in March and then returns at 02:00AM (“fall back”) on the first Sunday in November

要检查时间是否有效,您可以使用:

TimeZoneInfo.IsInvalidTime

关于c# - 日期时间解析错误 : The supplied DateTime represents an invalid time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36422138/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com