Asp.net web API change JSON date format to UTC

How can I automatically change the JSON format date

to UTC

?

now when i get data from the dates included in the server it is returned as local zone date. I know that I can change dates

on the server with this code to UTC

:

var date = DateTime.Now.ToUniversal();

      

but I need to do it automatically.

+3


source to share


1 answer


Json.NET

Keeps the time zone by default . You can override this by setting the property DateTimeZoneHandling

:

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;

      



Call this from your method Application_Start

defined in Global.asax.

+4


source







All Articles