Closed moriyaki closed 1 year ago
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis See info in area-owners.md if you want to be subscribed.
Author: | moriyaki |
---|---|
Assignees: | - |
Labels: | `area-System.Text.Json`, `untriaged` |
Milestone: | - |
var today = DateTime.Parse("2022-03-22");
That's because this isn't actually giving you what you think it is. It's parsing the date as a Japanese-era year date, not the Gregorian year date. If you're trying to parse an ISO/Gregorian date, use DateTime.Parse("2022-03-22", CultureInfo.InvariantCulture);
Remember that DateTime
doesn't "remember" culture calendars, and is strictly a representation of the ISO/Gregorian calendar. If you do today.Year
, it's going to give you 4040
.
@moriyaki
@Clockwork-Muse is correct. You are parsing the date using default CurrentCulture (which it seems in your case is the Japanese culture using Japanese calendar). The time will parse to equivalent Japanese date and as indicated DateTime doesn't carry any information regarding the associated calendar. Using DateTime.Parse("2022-03-22", CultureInfo.InvariantCulture);
should fix your issue.
Description
System.DateTime.Parse works fine, but when I pass the data through JsonSerializer, the date (year) is wrong.
Reproduction Steps
Expected behavior
2022/03/22 0:00:00 {"Date":"2022-03-22T00:00:00"}
Actual behavior
2022/03/22 0:00:00 {"Date":"4040-03-22T00:00:00"}
Regression?
No response
Known Workarounds
No response
Configuration
.net 6.0 Windows10 Pro(Japanese, and the Japanese Calender) x64
Other information
No response