Converting UNIX time to datetime object in Jinja templates

Leonard Mark picture Leonard Mark · Mar 1, 2013 · Viewed 13.7k times · Source

I want to convert my timestamp to datetime in jinja2..

here's my sample code:

import time

date = time.time()
self.tv['date'] = date

sample html:

<p>{{ date }}</p>

I want to convert it to datetime using jinja2 in python..

thanks..

Answer

KyungHoon Kim picture KyungHoon Kim · Feb 23, 2015

Make a custom filter like

@app.template_filter('ctime')
def timectime(s):
    return time.ctime(s) # datetime.datetime.fromtimestamp(s)

And use your template filter

{{ date | ctime }}