0

I'm posting this to an ASP.net Core action...

{
   "Start":"2018-09-30T00:00:00+00:00",
   "Finish":"2018-10-01T00:00:00+00:00"
}

It models binds to two Date? properties but the values are the same date but the time is 1am.

Why might this be? It only seems to do it when the "+00:00" is present so my guess is that this represents Greenwich meantime, which the server is currently at +1 for summer time.

Ian Warburton
  • 15,170
  • 23
  • 107
  • 189
  • Hi Ian, I assume you are using a custom model binder; would you be kind enough to post the code that is parsing these? – Brian Driscoll Sep 22 '18 at 23:29
  • I'm not using a custom model binder. I presume that what is running is the standard ASP.net Core model binder for dates. – Ian Warburton Sep 22 '18 at 23:31
  • 1
    I was afraid you’d say that... honestly you’d be better off passing the dates as strings to your controller and parsing them in code. – Brian Driscoll Sep 22 '18 at 23:42
  • Seems to work ok if I replace "+00:00" with empty string when generating the JSON. Probably a safe way of doing this using a custom serializer with Newtonsoft JSON. – Ian Warburton Sep 22 '18 at 23:48
  • The other thing you can try is changing the model property types to DateTimeOffset instead of DateTime – Brian Driscoll Sep 23 '18 at 00:11

1 Answers1

0

I essentially used this answer... https://stackoverflow.com/a/38276403/221683

This custom formatter doesn't include the time zone in the date string.

public class DateTimeConverter : DateTimeConverterBase
{
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
    {
        writer.WriteValue(((DateTime) value).ToString("yyyy-MM-ddTHH:mm:ss"));
    }
}

var serializedItem = JsonConvert.SerializeObject(item, new DateTimeConverter());
Ian Warburton
  • 15,170
  • 23
  • 107
  • 189