Default date serialization drops trailing zeros in the microseconds date portion.
See original GitHub issueHello,
I picked this problem up when I was parsing a date serialized by newtonsoft in another system.
I have a date from my source (.net system) as follows:
2017-11-21 05:55:27.064100
When this date is serialized by newtonsoft it comes out like this:
{"DateCreated":"2017-11-21T05:55:27.0641"}
Note the trailing zeros were truncated. This breaks deserialization which is expecting microseconds in my java app using this format specifier: “yyyy-MM-dd’T’HH:mm:ss.SSSSSSX”
I expect date to be serialized like this:
{"DateCreated":"2017-11-21T05:55:27.064100"}
I have attached code to reproduce. newtonsoft-date-issue.zip
Thanks,
Avner
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Ending 0's are getting truncated from millisecond part of ...
This issue is caused by the fact that Jackson by default uses Java's built-in DateTimeFormatter to serialize and deserialize dates and times ...
Read more >83989: Timestamp.toString() truncates trailing zeros in ...
In my case the MySql column is specified "timestamp datetime(3) DEFAULT NULL", and the result of .toString is typically "yyyy-mm-dd hh:mm:ss.fff ...
Read more >Keep trailing zeros on Timestamp SELECT
This is the issue: I am reading rows with a timestamp column from a table and I need to make sure that the...
Read more >DateTime and DateTimeOffset support in System.Text.Json
If the round-trip format representation of a DateTime or DateTimeOffset instance has trailing zeros in its fractional seconds, then ...
Read more >DateTime in chrono - Rust
Parses a string from a user-specified format into a DateTime<FixedOffset> value, and a slice with the remaining portion of the string. Note that...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I just discovered this myself. Truncating those 0’s also compromises sortability of the default date format.
I understand this is just a default and it is easy to override, but does anyone have any historical perspective on how this decision was reached? (I wanted to mimic Json.NET’s default date serialization behavior in a library of my own, assuming it represents best practices, but now I’m not so sure.)
After some thought, I think that the current default is probably best, and if we need datetimes that are sortable/comparable, we should override the time format string. See https://github.com/JamesNK/Newtonsoft.Json/issues/2780#issuecomment-1363704872 for more details 🙂