I have a datetime in the database that I read using SqlDataReader and then pass it to (DateTime). After casting its property Kind DateTimeKind.Unspecified.
(DateTime)
Kind
DateTimeKind.Unspecified
Then I have another line that I am reading from another source. Its format is as follows: 2016-01-20T22:20:29.055Z. I do DateTime.Parse("2016-01-20T22:20:29.055Z"), and his Kind property is DateTimeKind.Local.
2016-01-20T22:20:29.055Z
DateTime.Parse("2016-01-20T22:20:29.055Z")
How to analyze both days for comparison? Do I need to use DateTimeOffsets? How to disassemble it?
thanks
SQLReader DateTimeKind, . DateTime.SpecifyKind, DateTimeKind SQLReader . , UTC ; DateTimeOffset , SQL.
DateTime.SpecifyKind
"2016-01-20T22: 20: 29.055Z" ISO 8601 UTC; DateTime.Parse 1 . :
DateTime.Parse
, Parse DateTime, DateTimeKind.Unspecified. Parse -, s :
. UTC gotchas .NET SQL Server Derek Fowler .
2016-01-20T22:20:29.055Z ; "Z" , (UTC). , DateTime.Parse() DateTimeKind.Local, . DateTime.ParseExact .
DateTime.Parse()
DateTimeKind.Local
DateTime.ParseExact
, datetime Unspecified, , . , , "Z" , , 2016-01-20T22:20:29.055-07:00 (UTC-7).
Unspecified
2016-01-20T22:20:29.055-07:00
You can use something like this:
string format = "ddd dd MMM h:mm tt yyyy"; DateTime dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
In the format variable, you can put the desired format and pass it to the ParseExact function.
Hope this helps.
The database does not have a context (offset) of the date and time. You should save it either in the datetimeoffset column or in the datetime column, but keeping utc time. And it is always better to compare two utc times.
Source: https://habr.com/ru/post/1625224/More articles:Kivy - create a package on Windows - pythonggplot2: adding a new function and forcing it to return - rVideo.js does not work in Safari (CODE: 4 MEDIA_ERR_SRC_NOT_SUPPORTED) - javascriptHow to execute a method once before running maven surefire - javaВидео HTML5 с видео .js не работает в Safari - safariVideo.js jQuery нарушает функции воспроизведения видео/паузы в Firefox - javascriptTypes of access connections outside of the ad module in Elm - elmProgrammatically change X / Y center alignment constraint multiplier - iosVideo.js jQuery disrupts video playback / pause in Microsoft Edge - javascriptIs there a way to update a virtual replay container? - angularjsAll Articles