John Little
John Little

Reputation: 878

How to include time zone in Web API 2 JSON dates?

How do I configure the JSON dates produced by my Web API 2 Controller to include the time zone? Data type used for dates in SQL Server are datetime and I don’t have the option of changing the Legacy database.

Upvotes: 2

Views: 897

Answers (2)

Jay Traband
Jay Traband

Reputation: 17052

Breeze uses Json.NET to serialize/deserialize json. You can configure the serializer settings that Breeze uses by creating a custom class that inherits from Breeze.ContextProvider.BreezeConfig. Breeze will automatically discover this class and create an instance of it for all configuration tasks.

Something like this:

public class CustomBreezeConfig : Breeze.ContextProvider.BreezeConfig
   {
    protected override Newtonsoft.Json.JsonSerializerSettings CreateJsonSerializerSettings()
    {
        var ret = base.CreateJsonSerializerSettings();
        ret.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc; 
        //  ret.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local; 
        return ret;
    }        
}

But before you go down this path please read this (the response specifically):

breezejs: date is not set to the right time

Upvotes: 1

djikay
djikay

Reputation: 10628

Try returning your DateTime formatted with .ToString() and use a custom date and time format like "K". See: http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx#KSpecifier for more information.

Upvotes: 0

Related Questions