Is there a universal way to instruct the DataContractJsonSerializer to use UTC for dates? Otherwise, I have to add .ToUniversalTime() to all of my date instances. Is this possible? The reason is that date values are defaulting DateTimeKind.Local and adding offsets to the JSON result. Making the dates universal does the trick, but can it be done at a global level? Thanks.
Is there a universal way to instruct the DataContractJsonSerializer to use UTC for dates?
Share
There’s no way to do that directly at the global level – primitive types (such as DateTime) can’t be “surrogated”. A possible workaround is to use some kind of reflection along with a surrogate to change the DateTime fields (or properties) in an object when it’s being serialized, as shown in the example below.