Advanced date and time formatting
Date and time formatting is a painful thing to handle in web applications. Such formatting in Python, using the datetime
library, often increases overhead and is pretty complex when it comes to the correct handling of time zones. It is a best practice to standardize timestamps to UTC when they are stored in a database, but this means that the timestamp needs to be processed every time it is presented to users around the world.
Instead, it is smarter to defer this processing to the client side – that is, the browser. The browser always knows the current time zone of its user and will, therefore, be able to manipulate the date and time information correctly. This approach also reduces any unnecessary overhead from the application servers. In this recipe, we will understand how to achieve this. We will use Moment.js
for this purpose.
Getting ready
Moment.js
can be included in our app just like any JS library. We just have to download and...