官术网_书友最值得收藏!

  • Flask Framework Cookbook
  • Shalabh Aggarwal
  • 376字
  • 2021-08-05 17:17:22

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 following statement along with other JS libraries:

<script src="/static/js/moment.min.js"></script>

The basic usage of Moment.js is shown in the following code. This can be done in the browser console for JavaScript:

>>> moment().calendar();
"Today at 4:49 PM"
>>> moment().endOf('day').fromNow();
"in 7 hours"
>>> moment().format('LLLL');
"Tuesday, April 15 2014 4:55 PM"

How to do it…

To use Moment.js in our application, the best way will be to write a wrapper in Python and use it via jinja2 environment variables. Refer to http://runnable.com/UqGXnKwTGpQgAAO7/dates-and-times-in-flask-for-python for more information:

from jinja2 import Markup

class momentjs(object):
    def __init__(self, timestamp):
        self.timestamp = timestamp

    # Wrapper to call moment.js method
    def render(self, format):
        return Markup("<script>\ndocument.write(moment(\"%s\").%s);\n</script>" % (self.timestamp.strftime("%Y-%m-%dT%H:%M:%S"), format))

    # Format time
    def format(self, fmt):
        return self.render("format(\"%s\")" % fmt)

    def calendar(self):
        return self.render("calendar()")

    def fromNow(self):
        return self.render("fromNow()")

We can add as many Moment.js methods as we want to parse to the preceding class as and when needed. Now, in our app.py file, we can set this created class to the jinja environment variables:

# Set jinja template global
app.jinja_env.globals['momentjs'] = momentjs

We can use it in templates as follows:

<p>Current time: {{ momentjs(timestamp).calendar() }}</p>
<br/>
<p>Time: {{momentjs(timestamp).format('YYYY-MM-DD HH:mm:ss')}}</p>
<br/>
<p>From now: {{momentjs(timestamp).fromNow()}}</p>

See more

主站蜘蛛池模板: 尼勒克县| 简阳市| 湛江市| 新沂市| 卢龙县| 芦山县| 涪陵区| 栖霞市| 滦南县| 营山县| 龙游县| 和林格尔县| 泽州县| 平顶山市| 崇文区| 平原县| 游戏| 星子县| 日喀则市| 乌审旗| 南川市| 嘉峪关市| 怀安县| 肥城市| 工布江达县| 焉耆| 无锡市| 临湘市| 洪江市| 武鸣县| 大城县| 阳原县| 莎车县| 嵩明县| 辽中县| 娱乐| 霍城县| 拉萨市| 伊宁县| 汝南县| 冀州市|