0

I have a service which returns a json like this :

[  
   {  
      "title":"First event",
      "startDate":"\/Date(1495512000000-0400)\/",
      "endDate":"\/Date(1495857540000-0400)\/",
   }
]

I deserialize the json output to the list with respect to the model I have defined :

Model

public class EventModel
{
    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("startDate")]
    public DateTime StartDate { get; set; }

    [JsonProperty("endDate")]
    public DateTime EndDate { get; set; }
}

C# Code of deserialization

I am using Newtonsoft.Json package for deserialization.

List< EventModel > lstEvent = new List< EventModel >();
lstEvent = JsonConvert.DeserializeObject<List< EventModel >>(await objMyServices.GetData());

So in this case when I debug I get the following output in two different timezone:

For GMT+5:30 (Kolkata - India):

Start Time : 23-05-2017 09:30:00

End Time : 27-05-2017 09:29:00

For GMT-4:00 (New York):

Start Time : 23-05-2017 00:00:00

End Time : 26-05-2017 23:59:00

Therefore for every record, start time is not parsed to the respective system time zone when I switch to GMT-4:00 (New York). What is the thing I am missing here ?

Himanshu Dwivedi
  • 7,934
  • 3
  • 31
  • 52
  • That's not a Json date. JSon dates are formatted according to ISO8601. That's a very old format from the AJAX days. Some *very* old services still use it though – Panagiotis Kanavos May 25 '17 at 11:57
  • Furthermore, you are using DateTime which can be either Local, UTC or Undefined. What is the DateTimeKind property of the returned dates? The same UTC time will appear differently if converted to local in New York, yet it's still the same time. It will also appear different when displayed if you don't explicitly specify what DateTimeKind to use – Panagiotis Kanavos May 25 '17 at 11:58
  • @PanagiotisKanavos - I believe it *should* use ISO8601, but there's nothing that requires it: https://stackoverflow.com/questions/10286204/the-right-json-date-format – CodeNaked May 25 '17 at 12:01
  • @CodeNaked it's the defacto standard, which is the same – Panagiotis Kanavos May 25 '17 at 12:05
  • I am developing an app in xamarin, so when ever the device's time zone changes, it should automatically get converted to the current time zone of the device. The api's are old that I agree. But it can't be changed. How can I figure out to solve this issue. – Himanshu Dwivedi May 25 '17 at 12:06
  • If you need to keep UTC offset information then use `DateTimeOffset` instead of `DateTime`. – Federico Dipuma May 25 '17 at 12:30
  • Which of the two values do you want? The string contains New York time. The two local times are the correct representation of that - they do point to the same instant. If someone sends you an appointment in New York time, what do you want to do in your application? – Panagiotis Kanavos May 25 '17 at 13:18
  • I want for New York, once I change my device's timezone to New York. It should add an event corresponding to New York local time zone. – Himanshu Dwivedi May 25 '17 at 13:21

2 Answers2

1

Json.NET allows you to specify what kind of DateTime to return (UTC, Local) through the DateTimeZoneHandling serializer setting. The default is Local.

To return a UTC DateTime, use DateTimeZoneHandling.Utc :

var settings = new Newtonsoft.Json.JsonSerializerSettings() 
               {
                   DateTimeZoneHandling = DateTimeZoneHandling.Utc 
               };
var content=await objMyServices.GetData();
var lstEvent = JsonConvert.DeserializeObject<List<EventModel>>(content, settings);

Perhaps a better option is to change the property types to DateTimeOffset. This type preserves the original offset.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • When I changed the property to DateTimeOffset I am not getting the time. Output is : StartDate {23-05-2017 00:00:00 -04:00} – Himanshu Dwivedi May 25 '17 at 12:47
  • Isn't that the actual value, `23-05-2017 00:00:00 -04:00`? And the second one `26-05-2017 23:59:00 -04:00` ? – Panagiotis Kanavos May 25 '17 at 12:54
  • Yes, How will i get the local time and date. Since it is is UTC. I need to add the event on local date and time to device calendar – Himanshu Dwivedi May 25 '17 at 13:13
  • DateTimeOffset contains the actual offset. It's NOT UTC. By definition, UTC means the offset is 0. If you need the local time though, why the question? The values you posted correspond to the *correct* local times. Midnight in New York *is* 9:30 am in Kolkata. Your original code worked just fine in this case. – Panagiotis Kanavos May 25 '17 at 13:15
  • Anyway, you can get a DateTime value from a DateTimeOffset with `.LocalDateTime` or `.UtcDateTime`. You have to decide which one you want first though – Panagiotis Kanavos May 25 '17 at 13:17
  • Thanks @panagiotis. So for the value in json it will be 9:30 AM in Kolkata and 12:00 AM in New York. – Himanshu Dwivedi May 25 '17 at 13:46
0

You can use Ticks in Web API for this. This is much convenient when you are working with multiple time zones. Refer below example .

DateTime.UtcNow.Ticks
CuriousGuy
  • 3,818
  • 4
  • 24
  • 28