An example library to look into in PHP is called Carbon.īy following these rules, you should instill an efficient PROCESS in how you handle datetimes. An example library to look into in JavaScript is called moment.js. These make it a lot easier to do conversions, or formatting based on standards (e.g. There exist libraries in all major web development languages/frameworks for better handling of datetimes.
CODE ON TIME JAVASCRIPT RULE ISO
When you send requests to the backend, send the datetime in ISO 8601 format so that the backend can easily convert it to the corresponding UTC date time. Stay consistent with the format of your date times in the frontend by using a standard, such as ISO 8601.
Doing this instills a separation of duties between the backend (handle in UTC) and the frontend (handle in the user's local time). Although your backend will be returning UTC times, the frontend can easily convert these to the user's local timezone. Rule #2 - CONVERT DATETIMES TO THE USER'S LOCAL TIMEZONE USING FRONTEND CODE.
Furthermore, make sure the datetime is in UTC when you're handling it with your backend code. The major timezones are just offsets from UTC. If you are not familiar with what UTC is, it is a primary time standard that all the major timezones are based on. When storing dates in the database, they should always be in UTC. It is important that there is consistency across all your date-related data. Rule #1 - STORE DATETIMES IN UTC IN YOUR DATABASE, AND BACK END CODE. I think by following these simple rules, it can be a lot less of a headache for you. My guess is that the people who said "NO" to this poll either have it figured out already, or they don't work with platforms/apps that have to cater to users across the globe.Īnyway, I wanted to create a blog post that goes into some detail on how I handle dates and timezones. When asked if they struggle with handling dates and timezones, more than 50% of developers responded "YES".