Jim Reineri
Jim Reineri

Reputation: 2980

Timezone shift of JSON data in web service

I have an ASP.NET MVC3 Restful web service that uses the Microsoft JSON serializer. This service returns data that contains a .NET DateTime value.

The web service is accessed by a Silverlight client that uses the Newtonsoft JSON library to deserialize the returned data.

The date value that I get in the client are five hours ahead of the value that are sent from the service. Since I am in the Eastern Time Zone (US), this appears to be localtime being sent from the service getting interpreted as GMT by the client.

My question is this: What is a good way to handle this discrepancy? Is there something in either the Microsoft or the Newtonsoft library that I can set to deal with this. Something a little more elegant than subtracting 5 hours from the time received by the client.

Thanks

Upvotes: 0

Views: 598

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340973

First have a look at this question (it is about Backbone.js, but applies to your problem as well): How to handle dates in Backbone?

Some libraries (like Jackson) serialize dates to UNIX time by default. How is the date/time represented in the data sent from the server? If it is not a simple integer, it should be represented using ISO 8601 which always explicitly defines timezone (or Z for UTC time).

If the time is sent in textual form from the server, but without time zone, the server marshals it incorrectly. If the time has correct time zone but the client discards it - it is the client fault.

Upvotes: 1

Related Questions