As developers, it's easy to think that working with dates and times is strictly a computer science problem. Even if it sometimes looks like someone is playing a cruel joke on programmers, historically making sense of time was a challenging task, much more than trying to parse some weird local datetime format. Tom Scott has a great video about problems with Timezones:
Two of the most prevalent datetime formats for string representation are ISO 8601 and Unix Timestamp. Both formats are widely adopted and have distinct advantages and disadvantages. ISO 8601 is a human-readable, while Unix Timestamp offers a compact representation that simplifies arithmetic operations.
What is "a perfect date" (in string format)?
- Humanly readable - The format clearly defines the order of year, month, and day and the time in the context we humans use.
- Sortable - It should be possible to sort dates only by comparing strings (and not have to convert to Date types).
- Time zone support - "A perfect date" should (probably) have a time zone support
- In most cases, I don't care about time zone support. I'm just using UTC. However, there are legit cases when we want to compare local times.
- Compact - date should be as compact as possible.
- Few chars for a few dates may seem like little, but imagine you must store millions of dates.
- Arithmetic operations - "A perfect date" should be able to add or subtract time easily
ISO 8601 - The most popular format
Human-readable - ISO 8601 represents dates and times in a clear and easy-to-understand format. Example:
2023-03-21T14:30:00Z
or with time zone offset:2023-03-21T14:30:00-07:00
- However, there is a decent chance you could make a mistake when editing and still keep it "humanly readable," like
2023-03-21 T14:30:00Z
.
- However, there is a decent chance you could make a mistake when editing and still keep it "humanly readable," like
Sortable - ISO 8601 is sortable datetime format.
Time zone support - ISO 8601 supports time zones, either as an offset (e.g.,
2023-03-21T14:30:00-07:00
) or as Coordinated Universal Time (UTC, indicated by the "Z" at the end, e.g.,2023-03-21T14:30:00Z
).Compact - ISO 8601 is not compact
Arithmetic operations - To add or remove the time of date time in ISO 8601, you have to convert it to date time, or in more straightforward cases, you can do it manually.
Unix Timestamp - The most compact (and cool) format
- Human-readable - Unix Timestamps are not humanly readable
- But, are easier to "paste."
- Sortable - Unix Timestamps are sortable
- Time zone support - There is no support for Time zones.
- However, one could argue that we should not pack time zone information in datetime format but use UTC and time zone (or location) information separately
- Compact - Unix Timestamp is compact -
1681045331
- More than two times fewer characters
- We could convert it to a number
- However, we could also convert ISO 8601 to
datetime
in most programming languages.
- However, we could also convert ISO 8601 to
- Arithmetic operations - You have to convert it to a number, but after that, adding and removing times are quick (you are dealing with numbers).
Date and time notation in the United States
MM/DD/YYYY hh:mm AM/PM (e.g., 04/09/2023 02:30 PM)
Custom Formats
Sometimes we use custom date formats tailored to our specific requirements. While they may serve the purpose of a particular application, they may not be standardized.
Conclusions
What is "the best" datetime format?
It depends...
The safest path to go is to use ISO 8601. In cases where size is more critical than (human) readability, use Unix Timestamp. For special cases, create something custom!