0

This post is meant to be language agnostic. What should you have in mind, when designing APIs having dates in them? What could be a possible advantage of having: 2016-11-01T20:44:39Z instead of timestamp in milliseconds like this: 1583749154495

Accoding to What pattern should be used to parse RFC 3339 datetime strings in java, even RFC 3339 format can differ. Why not use timestamps in all places, for example when building REST backend APIs? Another possible advantage of that, is that you send timestamp as a number to front-end, and then front-end libraries take care of things like Time Zone, displaying adjusted date output to each individual user.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Dargenn
  • 372
  • 4
  • 11
  • While seconds since the epoch might be regarded as pretty universal (though they come from Unix and other OSs have other ways), milliseconds or microseconds are more biased. The completely language and platform neutral is [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). Your example `2016-11-01T20:44:39Z` is ISO 8601. – Ole V.V. Mar 09 '20 at 20:53

1 Answers1

6

I would definitely recommend using a UTC datetime to indicate a particular point in time, even if a timestamp is a more terse format. Things to consider though are:

  1. Readability: Your UI and/or backend services aren't the only consumers of those timestamps. These timestamps end up in logs, and a human-readable format helps a lot
  2. Compatibility: using an integral representation of time may lead to bugs depending on method & platform used: https://en.wikipedia.org/wiki/System_time
  3. Size of contract: if you send massive amounts of timestamps, then a more terse format like integral timestamps may be beneficial. Practically this shouldn't be a concern in 99.9% of cases.

I would definitely suggest you stick to a known, readable, universal standard for representing time, unless your needs clearly dictate you moving away from that standard.

Savvas Kleanthous
  • 2,695
  • 17
  • 18
  • 3
    *a known, readable, universal standard* — sounds to me like you said [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) only without mentioning the ISO number. :-) – Ole V.V. Mar 09 '20 at 20:49