Converting a string to a date using C # in ASP.NET gives me a date by the hour. What for?

I code the following using VS2010, C #, ASP.NET:

DateTime dt = DateTime.Parse("2012-03-11T02:53:58-08:00"); //Date is taken from SQL database string strDt = dt.ToString(); //Becomes: "3/11/2012 3:53:58 AM" 

Is this a joke in April from Microsoft?

+4
source share
3 answers

The โ€œincorrectโ€ result (as the answer to the dark suit explains) is due to the fact that on March 11, 2012, 2:38:58 a.m., there was no valid date and time in areas of the United States and Canada that observe daylight saving time. Obviously, your code runs on a computer in one of these areas.

To convert a string to DateTime , ignoring any timezone offset, you can call DateTimeOffset.Parse and then get the DateTime component of the result:

 DateTime dt = DateTimeOffset.Parse("2012-03-11T02:53:58-08:00").DateTime; string strDt = dt.ToString(); // "3/11/2012 2:53:58 AM" 

UPDATE:. What is the difference between DateTime.Parse and DateTimeOffset.Parse when the source string contains a timezone offset? Consider these two examples, which assume your current time zone is Pacific Time:

 // Example 1: DateTime.Parse(String) DateTime dt = DateTime.Parse("2012-03-11T06:00:00-04:00"); Console.WriteLine(dt.ToString("o")); // 2012-03-11T03:00:00.0000000-07:00 

DateTime.Parse uses an offset to edit the syntax date and time local . Please note that the time has changed from 6:00 to 3:00, which reflects the transition from Eastern daylight (UTC-04: 00) to Pacific daylight (UTC-07: 00). In your question, the time has changed because DateTime.Parse automatically adjusted the time from Pacific Standard Time (UTC-08: 00) to Quiet Daylight Time (UTC-07: 00).

 // Example 2: DateTimeOffset.Parse(String) DateTimeOffset dto = DateTimeOffset.Parse("2012-03-11T06:00:00-04:00"); Console.WriteLine(dto.DateTime.ToString("o")); // 2012-03-11T06:00:00.0000000 Console.WriteLine(dto.Offset); // -04:00:00 

DateTimeOffset.Parse easier. It simply returns a DateTimeOffset value whose DateTime and Offset properties are set to parse date, time, and offset. But be careful: If the time zone offset in the line does not match the time zone you want to work with, then you need to set the date and time yourself.

+4
source

This allows you to use the time zone in which you are located. The time string is in ISO8601 format with a time zone.

http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

+4
source

This is not a joke or a problem with the time zone. It is summer time. Google "Daylight Saving Time 2012", here is the result:

Daylight saving time (US) began on Sunday, March 11, 2012, 2:00 and ends on Sunday, November 4, 2012, 2:00. Except Arizona and Hawaii. Move your watch forward 1 hour in spring and back 1 hour in the fall ("Spring ahead, back off").

In other words, wherever daylight saving time is observed, no 2:53:58 AM on March 11, 2012. Hours are skipped after 2 hours from 1:59:59 to 3: 00:00 PM. Why is your sql datetime not observing daylight saving time and the machine running C # is another problem.

+3
source

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


All Articles