DateTime rounding struggles
While writing tests for my api, I've noticed an interesting issue.
My assertion comparing an object returned by an api with a reference in the database kept failing, while they were querying the same object in the database.
The difference was apparently a microsecond property of
DateTime
that did not make it through Json serialization, while EF core returned the full value, thus the objects were different.
Is there a way i could modify serialization properties in asp.net api to transmit the full timestamp without sacrificing data integrity? Or somehow round value to disregard the millisecond property?2 Replies
Json itself doesn't care for dates. You can serialize it as a string if you want, as long as your consistent on the other end. I would recommend staying with a good ISO though.
ISO8601 represent
Your best bet would be to either:
1. Compare Unix timestamps since they have a maximum precision of seconds
2. Write an extension method that strips microseconds and compare that
3. Write an extension method that compares only the desired elements of the DateTime