2

I am using Servicestack and Ormlite for my project and testing with postman. The C# type I am using for my timestamps is DateTime and it processes the info correctly to and from the MySql database. When I return the value in my response the value looks correct but when the response is inspected in either Postman or just browsing to the the request url the value I see is [ { "id": 1, "accountType": 1, "name": "Mr J Smith", "memberSince": "/Date(1539189581000-0000)/", "phone": "8138138138", "email": "jjj@jjj.com", "emailVerified": false } ] The value I send out is MemberSince = {10/10/2018 12:39:41 PM}

Why the discrepancy? Is it a different format? How do I globally convert it?

UPDATE

I add this to my initialization routine and it works. Is this the best way to do this?

            JsConfig<DateTime>.SerializeFn = time => new DateTime(time.Ticks, DateTimeKind.Utc).ToString("o");
        JsConfig<DateTime?>.SerializeFn = time => time != null ? new DateTime(time.Value.Ticks, DateTimeKind.Utc).ToString("o") : null;
        JsConfig.DateHandler = DateHandler.ISO8601;
Jeff
  • 2,061
  • 4
  • 27
  • 45
  • Possible duplicate of [How do I format a Microsoft JSON date?](https://stackoverflow.com/questions/206384/how-do-i-format-a-microsoft-json-date) – Norsk Oct 11 '18 at 01:28
  • @Norsk I don't think so, this looks like a JavaScript UTC date type – John Baker Oct 11 '18 at 01:32
  • @Jeff is it possible you have a filter on Controller or Endpoint that is setting the output for DateTime's. It isn't typical, but the Date output that is used is JavaScript's UTC date type – John Baker Oct 11 '18 at 01:34
  • I don't think so. I am using the serializer that is part of ServiceStack – Jeff Oct 11 '18 at 01:34
  • I AM storing the DateTimes as UTC (DateTime.UtcNow) if that helps. – Jeff Oct 11 '18 at 01:41
  • Looks like it is the way ServiceStack serializer works. https://stackoverflow.com/questions/19694475/servicestack-is-there-a-way-to-force-all-serialized-dates-to-use-a-specific-da – JamieMeyer Oct 11 '18 at 01:43

2 Answers2

1

I suggest you to check the serializer you are using, it has nothing to do with postman, is the way you are returning the data from the backend.

For example:

  return Json(
                    new DataSourceResult
                    {
                        Data = "testing"
                    });

When the serializer sends this data the word Data on the ajax/javascript side I need to read it as data in lower case, because the serializer set the first letter to lower case. So first of all, see what the serializer sends over the wire, and also try to change on your backend the format of your DateTime. As I am seeing the serializer takes all DateTime and convert them in that format. Try to send instead of the DateTime a string representation of the date and see what you receive on the other side. Should be the plain string. Hope this helps

Zinov
  • 3,817
  • 5
  • 36
  • 70
  • I see that the info here is related but it does not answer the question. See my update above. – Jeff Oct 11 '18 at 01:52
1

You can configure ServiceStack.Text to use the ISO8601 Date format with:

JsConfig.Init(new Config {
    DateHandler = DateHandler.ISO8601
});
mythz
  • 141,670
  • 29
  • 246
  • 390