0

I have web api 2.0 controller method, which returns my objct as json:

return this.Ok(myObject);

Everything works perfect. But there is one problem. One property on myObject is date:

public DateTime Dt { get; set; }

And if I inspect Dt property in debugger(before return ok), there is correct 430 milliseconds. But json returned from server has date in format:

2016-06-02T11:56:38.43

the last zero number is missing. It should be:

2016-06-02T11:56:38.430

In case there is something different than zero, then json has all 3 miliseconds, like:

2016-06-02T11:56:38.435

So, why web api removes last number if it is 0 when creating json and how to correct that?

Simon
  • 1,955
  • 5
  • 35
  • 49
  • 2016-06-02T11:56:38.43 and 2016-06-02T11:56:38.430 are equivalent. If you want to show the trailing zero -- that should be done via formatting in your html. – dbugger Jun 02 '16 at 14:28
  • I would like that server already returns trailing zero - how can i do that? On client side - some clients can recognize 43 as date while some other clients don't - based on regional settings. While 430 all clients recognize as date. Parsing string on client and adding 0 to the end if missing - for that I have to change 3'd party library code - what I would like to avoid. – Simon Jun 03 '16 at 07:32
  • 2
    Possible duplicate of [Force JSON.NET to include milliseconds when serializing DateTime (even if ms component is zero)](http://stackoverflow.com/questions/18193281/force-json-net-to-include-milliseconds-when-serializing-datetime-even-if-ms-com) – dbugger Jun 03 '16 at 11:17

0 Answers0