Book Image

Flask Framework Cookbook

By : Shalabh Aggarwal
Book Image

Flask Framework Cookbook

By: Shalabh Aggarwal

Overview of this book

Table of Contents (19 chapters)
Flask Framework Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Advanced date and time formatting


Date and time formatting is a painful thing to handle in web applications. Handling them at the level of Python, using the datetime library increases the overhead and is pretty complex when it comes to handling time zones correctly. We should standardize the timestamps to UTC when stored in the database, but then, the timestamps need to be processed every time they need to be presented to the users worldwide.

It is a smart thing to defer this processing to the client side, that is, the browser. The browser always knows the current time zone of the user and will be able to do the date and time manipulation correctly. Also, this takes off the necessary overhead from our application servers. We will use Moment.js for this purpose.

Getting ready

Just like any JS library, Moment.js can be included in our app in the following manner. We will just have to place the JS file, moment.min.js, in the static/js folder. This can then be used in our HTML file by adding the...