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

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

主站蜘蛛池模板: 翁牛特旗| 石棉县| 金川县| 揭西县| 武义县| 榆树市| 宁夏| 都兰县| 沙坪坝区| 卓资县| 安仁县| 蓬溪县| 宁强县| 哈尔滨市| 延吉市| 来安县| 巧家县| 浮梁县| 山西省| 承德县| 汉中市| 广饶县| 青河县| 聂拉木县| 荣昌县| 土默特右旗| 平陆县| 炉霍县| 鄂托克前旗| 昆明市| 友谊县| 沾益县| 九台市| 内乡县| 兴文县| 滦南县| 河源市| 农安县| 洪雅县| 连江县| 中卫市|