keep C# datetime local time between json and Web api?

前端 未结 3 1908
死守一世寂寞
死守一世寂寞 2021-02-05 12:55

I have problem when I have datatime in json object it will convert it to UTC time zone in C# dateTime just want to ask how to keep local time?can I set time zone property in w

3条回答
  •  没有蜡笔的小新
    2021-02-05 13:20

    You can configure this. See: http://www.newtonsoft.com/json/help/html/SerializeDateTimeZoneHandling.htm

    Here's an example:

    public void Config(IAppBuilder app)
    {
        var config = new HttpConfiguration();
    
        var jsonFormatter = config.Formatters.OfType().First();
        jsonFormatter.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
    
        app.UseWebApi(config);
    }
    

提交回复
热议问题