GenesysStack.com

Did You Know that you can use standard ISO 8601 Dates with the DataContractJsonSerializer class?

Did You Know that you can use standard ISO 8601 Dates with the DataContractJsonSerializer class?

By default, the DataContractJsonSerializer class expects dates in the format of Date(ticks). This is problematic, because ASP.NET Web API returns dates in ISO 8601 format.

* Default date: "{\"BirthDate\":\\\/Date(248860800000-0800)\\/\

* ISO 8601 date: "{\"BirthDate\":\"1982-11-16T00:00:00-08:00\"}"

To use the ISO 8601 date standard with the DataContractJsonSerializer, specify the DateTimeFormat property format:

            DataContractJsonSerializer serializer = newDataContractJsonSerializer(typeof(T),

                    newDataContractJsonSerializerSettings() { DateTimeFormat = new                          

                         DateTimeFormat("yyyy-MM-dd'T'HH:mm:sszzz") });

 

All dates will now default to ISO 8601.

Leave your comment
Comments
12/24/2016 9:14 AM
I'm imseepsrd. You've really raised the bar with that.