'API Force Consistent Response Json Serialization

I have a .net framework 4.7.2 api for which I need all response DateTime and DateTime? values to be serialized per ISO 8601 in local time, but I have been unable to get it to format all dates consistently; some will be formatted correctly, but others will appear as "2022-02-24T00:00:00" (likely because some are coming from a sql database as tsql Date types, while others are generated inline with new DateTime(int, int, int).

The code below appears in my Global.asax.cs file as the last executing code, but it doesn't appear to be working as I'd expect.

I believe I could specify the DateTimeKind value for the dates manually, but this api is already complex; we don't need the maintenance headache of having to manually set this for all dto values.

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source