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