JsonSerializer DateTimeOffset Not Correct in Javascript
Closed this issue · 5 comments
Describe the issue
Hi,
I need convert DateTimeOffset and DateTime to Json With DateHandler.TimestampOffset but DateTimeOffset result not correct for Date in javascript
Reproduction
code in C#:
//Date Now : 9/29/2023 7:27:54 PM +03:30
var json1 = JsonSerializer.SerializeToString<DateTimeOffset>(DateTimeOffset.Now);
//json1 Result: Date(1696015674297+0330)
//Date Now : 9/29/2023 7:27:54 PM
var json2 = JsonSerializer.SerializeToString<DateTime>(DateTime.Now);
//json2 Result: Date(1696003074485+0330)now i need convert result to Date in javascript
var date1 = new Date(1696015674297); //json1 Result: Date(1696015674297+0330)
//Result: Fri Sep 29 2023 22:57:54 GMT+0330
var date2 = new Date(1696003074485); //json2 Result: Date(1696003074485+0330)
//Result: Fri Sep 29 2023 19:27:54 GMT+0330date1 (DateTimeOffset json) result not correct 3:30 hours ahead
date2 (DateTime json) result correct
Expected behavior
output json DateTimeOffset and DateTime must be the same
The only difference between them is that DateTime has no Offset
System Info
Windows 10
ServiceStack.Text 6.10Additional context
sry my english not good
Validations
- Confirm Issue is reproducible from the information provided
- Have an active commercial ServiceStack License (GitHub Username is registered on Support page)
i try use momentjs result not correct !
var date1 = moment("/Date(1696015674297+0330)/"); //DateTimeOffset json Result
//Result: Fri Sep 29 2023 22:57:54 GMT+0330 not correctThe serialized dates should be in UTC,.
You can find out how to parse WCF JSON dates in the JSON Format docs.
The Dates themselves are in UTC, it's up to you with how you want to handle the offsets.
Note: you've checked GitHub Username is registered on Support page, but I can't find a record for your GitHub Username, please ensure your GitHub username is listed as a registered support contact prior to opening issues.
JsonSerializer Automatic conversion of DateTime to UTC for TimestampOffset.
But it doesn't do this for DateTimeOffset.
i try use Newtonsoft.Json work result correct
Newtonsoft.Json.JsonConvert.DefaultSettings = () => new Newtonsoft.Json.JsonSerializerSettings
{
DateFormatHandling= Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat,
};
//Date Now : 9/29/2023 7:27:54 PM +03:30
var newtonj = Newtonsoft.Json.JsonConvert.SerializeObject(DateTimeOffset.Now);
//json2 Result: Date(1696003074485+0330) result correct
...This should be resolved in the latest v6.10.1+ that's now available in our pre-release packages.