14

My DateTimePicker is bound to property:

    picker.DataBindings.Add("Value", this, "EventDate");
    ...
    private DateTime eventDate;
    public DateTime EventDate
    {
        get
        {
            var offset = TimeZoneInfo.Local.GetUtcOffset(eventDate);
            string json = JsonConvert.SerializeObject(eventDate, Formatting.Indented);
            Console.WriteLine("get: {0} --- {1}", json, offset);
            return eventDate;
        }
        set
        {
            var offset = TimeZoneInfo.Local.GetUtcOffset(value);
            string json = JsonConvert.SerializeObject(value, Formatting.Indented);
            Console.WriteLine("set: {0} --- {1}", json, offset);
            eventDate = value;
        }
    }

Whenever data binding is to set the property, I've got the following result:

set: "2015-08-03T16:06:59" --- 04:00:00
get: "2015-08-03T16:06:59" --- 04:00:00

The code in get/set only for debug purposes. The whole class is serialized along with EventDate property.

How can I modify DateTime variable and/or json serializer in order to include timezone offset such as:

"2014-08-03T16:06:59.8708232+04:00"

Strange thing is if I create brand new DateTime object and assign to DateTime.Now without binding, JSON.NET will append timezone offset to it. Can't figure out what is the difference.

2 Answers 2

19

Have you tried type DateTimeOffset instead of DateTime? This is the type with timezone information.

var example = new
{
    Now = DateTimeOffset.Now,
    UtcNow = DateTimeOffset.UtcNow,
    Sometime = new DateTimeOffset(2014, 10, 11, 1, 4, 9, new TimeSpan(8, 0, 0)),
    FromDateTime = new DateTimeOffset(new DateTime(2010, 1, 1)),
};
string json = JsonConvert.SerializeObject(example, Formatting.Indented);
Console.WriteLine(json);

Gives me:

{
  "Now": "2014-08-03T22:08:47.8127753+08:00",
  "UtcNow": "2014-08-03T14:08:47.8137754+00:00",
  "Sometime": "2014-10-11T01:04:09+08:00",
  "FromDateTime": "2010-01-01T00:00:00+08:00"
}

EDIT - Alternative way

You noticed that DateTime.Now has timezone offset in JSON while manually created DateTime does not. This is because DateTime.Now has Kind equal to DateTimeKind.Local while the other has DateTimeKind.Unspecified, and JSON.NET just handles them differently.

So the alternative way is:

var product2 = new
{
    Now = DateTime.Now,
    Sometime = new DateTime(2014, 10, 11, 0, 0, 0),
    SometimeLocal = new DateTime(2014, 10, 11, 0, 0, 0, DateTimeKind.Local),
    SometimeUtc = new DateTime(2014, 10, 11, 0, 0, 0, DateTimeKind.Utc)
};

string json2 = JsonConvert.SerializeObject(product2, Formatting.Indented);

Console.WriteLine(json2);

Which outputs:

{
  "Now": "2014-08-03T23:39:45.3319275+08:00",
  "Sometime": "2014-10-11T00:00:00",
  "SometimeLocal": "2014-10-11T00:00:00+08:00",
  "SometimeUtc": "2014-10-11T00:00:00Z"
}

EDIT2 - For data binding

OK you use data binding. Then you can update your setter to convert automatically:

picker.DataBindings.Add("Value", this, "EventDate");
...
private DateTimeOffset eventDate;
public DateTime EventDate
{
    get { return eventDate.LocalDateTime; }
    set { eventDate = new DateTimeOffset(value); }
}

Then you can use eventDate to serialize to JSON.

Or to set Kind:

picker.DataBindings.Add("Value", this, "EventDate");
...
private DateTime eventDate;
public DateTime EventDate
{
    get { return eventDate; }
    set
    {
        // Create a copy of DateTime and set Kind to Local since Kind is readonly
        eventDate = new DateTime(value.Ticks, DateTimeKind.Local);
    }
}

Both should work.

Sign up to request clarification or add additional context in comments.

10 Comments

I'm not sure if I can go with solution, since DateTimePicker is not playing with DateTimeOffset.
@Pablo What DateTimePicker are you using? Can you convert DateTime to a DateTimeOffset in the callback?
using regular Windows Forms DateTimePicker from 4.0 Dot Net framework. Not sure what callbacks you refer..
Moreover, I don't understand why DateTime.Now producing desired result, but with binding my DateTime object is not good enough for JSON.NET to specify TZ.
@Pablo And for you 2nd question regarding DateTime.Now and manually constructed DateTime, the difference is DateTime.Now has Kind equal to DateTimeKind.Local while the other is DateTimeKind.Unspecified. So this is an alternative way to put this information in JSON. Please see my updated answer.
|
12

I use this.

It works very well.

JsonConvert.SerializeObject(object, new JsonSerializerSettings()
{
    DateFormatHandling = DateFormatHandling.IsoDateFormat,
    DateTimeZoneHandling = DateTimeZoneHandling.Local
});

1 Comment

How can you define this setting for the entire project?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.