7

Interesting question came up at work in regards to the better format of passing a date/datetime as a url parameter in an API: Is it better to pass as a unix timestamp, or as a plaintext date string (01/30/2015 04:17:57pm, 2015-01-27T16:17:57+00:00)?

jsanc623
  • 179
  • 1
  • 1
  • 6
  • 2
    "What are the pros and cons" [generally isn't a good question](http://meta.programmers.stackexchange.com/questions/6758/what-is-the-problem-with-pros-and-cons). – Doval Jan 30 '15 at 17:28
  • @Doval reworded – jsanc623 Jan 30 '15 at 17:35
  • 2
    You've changed the wording, but not the question. What is your definition of "better"? – Doval Jan 30 '15 at 17:41
  • Better = which format is more portable, which format is more widely used, which format is more widely accepted / acceptable, etc. Should I change the question entirely rather than rewording it to make it less broad? – jsanc623 Jan 30 '15 at 17:54

1 Answers1

9

It depends on the usage, but I'd probably say it's safest to convert to UTC then pass the ISO 8601 style string as you suggest.

That way, it's human-readable, and you don't have to worry about different time zones, etc.

While I was researching for an answer, I found an interesting answer on StackOverflow you might want to read: link.

(edit) Also, if you're using ISO 8601, there's no doubt about the interpretation of the format (DD/MM/YYYY vs. MM/DD/YYYY vs. YYYY/DD/MM vs. YYYY/MM/DD etc.).

Wai Ha Lee
  • 321
  • 3
  • 12
  • 2
    Just as an addendum to this answer, UNIX timestamps are limited in the epoch they can denote, they don't handle dates prior to 1970 very well. So an text string makes sense if you need to handle a very wide range of dates and times. – Jim Nutt Jan 31 '15 at 01:24
  • 2
    For anyone dropping in, ISO 8601 format is: `yyyy-MM-ddTHH:mm:ssZ` eg. `2008-09-22T14:01:54.9571247Z` (see https://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-an-iso-8601-date-in-string-format) – Adam Hughes Dec 20 '19 at 16:08