![]() |
| [image credit: Orca on pixabay dot com] |
(this post was prompted by a question posted on LinkedIn by Robin Moffat
A placeholder for organizing my thoughts on date handling...
Benefits of Using Native Date Types:
- Data Integrity: Native types like DATE, DATETIME, or TIMESTAMP enforce correct formatting and valid dates (e.g., preventing a date like February 30th).
- Performance: Native date types are typically stored as compact numeric values. This makes sorting and indexing significantly faster compared to string comparisons.
- Date Arithmetic: Using native types may allows you to easily perform operations like adding days, finding the difference between two moments, or extracting components (year, month, day) without complex string parsing.
- Standardization: Most relational databases and programming languages provide robust libraries for handling native dates, ensuring consistent behavior across different systems.
Best Practices:
- Always use UTC: Store all timestamps in Coordinated Universal Time (UTC) to avoid complexities with time zones and daylight saving changes.
- Convert at the Edge: Only convert dates to strings (formatted for a specific locale) when they need to be displayed to a human in the presentation tier.
When Storing/Transmitting Dates as Strings May Be Necessary:
- External API Constraints: When an external service only accepts or provides data in a specific string format (e.g., JSON which lacks a native date type).
- Historical Edge Cases: Some database DATE types do not support dates in the distant past. For example:
- SQL Server's DATETIME limit of 1753-01-01
- MySQL TIMESTAMP restrictions starting at 1970
- Lack of BCE (Before Common Era) support
Standard-based Date Format:
- Use ISO 8601 (i.e., YYYY-MM-DD)
Native Date Storage Considerations:
- C#
- ... TO-DO, add entries
- C/C++
- ... TO-DO, add entries
- Java
- ... TO-DO, add entries
- Python
- ... TO-DO, add entries
- Rust
- ... TO-DO, add entries
Other misc. articles/posts that may be of possible interest:
- Software Engineering Stackexchange: Why not return dates as a string from the database?
- Noah Sussman (LinkedIn):
- infiniteundo.com
- http://falsehoodsabouttime.com
- Falsehoods programmers believe about time
- https://www.linkedin.com/feed/update/urn:li:activity:7453872223800430593/
- https://www.linkedin.com/feed/update/urn:li:activity:7391815745963261952/
- https://infinite-undo.medium.com/falsehoods-programmers-believe-about-time-0033203f0410
- More falsehoods programmers believe about time; “wisdom of the crowd” edition
- Iain MacDonald (LinkedIn, Github)
