DateTime parsing error: provided DateTime represents invalid time

I have one situation where the date is "3/13/2016 2:41:00 AM" . When I convert the date clockwise, I get an error message.

 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); 

after execution, I get this error:

The supplied DateTime is an invalid time. For example, when the clock is adjusted forward, at any time during the period that is skipped is invalid. Parameter Name: dateTime

+5
source share
1 answer

Try checking if the time is ambiguous or valid. Due to the change in daylight, the time you talked about at 2:41:00 AM does not exist, because the clock was moved 1 hour ahead, and therefore the date is invalid or ambiguous.

 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 

You can also refer to this blog: System.TimeZoneInfo: working with ambiguous and invalid points in time

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

Background Information

Time zones that adjust their time for daylight saving time (in most cases, moving the time clockwise back or forward by 1 hour) have spaces and repeats on the timeline - wherever the time was moved forward or backward by adjusting. Use Pacific Standard Time as an example. In 2007, Pacific Standard Time (PST) changed to Pacific Daylight Time (PDT) at 02:00 a.m. ("spring forward") on the second Sunday in March, and then returns at 02:00 a.m. ("back off") on the first Sunday in November

To check if the time is valid, you can use:

 TimeZoneInfo.IsInvalidTime 
+7
source

Source: https://habr.com/ru/post/1246453/


All Articles