Date and DateTime Handling in APIs

Sharing is Caring

If you have been programming for a while, I’m sure that you’re aware of how painful dates and datetimes can be because of the different time zones. Application Programming Interfaces and dates are just as difficult.

Timezone issues crop up because dates and datetimes are just strings in JSON, there’s nothing to necessarily say that this is a date in this format.

Dates and times need to be dealt with consistently and appropriately. Internally, the API and database should process and store dates / datetimes in UTC or GMT. I recommend using the ISO 8601 time point format in the fully-enhanced format. The fully enhanced format includes hours, minutes, seconds, and miliseconds.

All requests and responses should use the exact same format for dates and datetimes.

It looks like this ‘yyyy-MM-dd’T’HH:mm:ss.SSS’Z’. So, May 29th 2022 at 12am UTC would look this: “2022-05-29T12:00:00.000Z”.

This helps with timezone issues because then the client translates them to their appropriate version. If the client is a webbrowser, it can translate the times automatically.

If it’s Node.js you can should read the article Dealing With Dates in JavaScript.

Sharing is Caring

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.