When using the fetch API to POST a JSON object as a string to a service method, I encountered an issue with timezones being converted. Initially, the object contained ISO 8601 strings with timezones (e.g. "StartDate": "2019-04-16T13:46:04-06:00"), but upon reaching the C# REST service method ([FromBody]object document), the timezone had changed to "StartDate": "2019-04-16T19:46:04+00:00".
I am puzzled as to why and where this conversion is happening. The data remains as a string throughout the process.
An interesting observation is that this behavior only occurs when deploying the service; everything works as expected when testing locally on localhost.
For reference, please find the example code snippets below:
postDocument() {
let doc = "{'StartDate': '2019-04-16T13:46:04-06:00'}";
let response = await fetch("[serviceURL]/api/Document/AddDocument",
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: doc
});
}
Server-side code in TestService (C#):
[HttpPost]
public void AddDocument([FromBody]object document)
{
// Datetime string has already been converted, no longer has timezone.
console.log(document.ToString());
// startdate here is = "2019-04-16T19:46:04+00:00"
return;//Doesn't matter what's in this method
}