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..
Timestamp to DateTime object You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.
Jinja supports dynamic inheritance and does not distinguish between parent and child template as long as no extends tag is visited.
What is Jinja 2? Jinja2 is a modern day templating language for Python developers. It was made after Django's template. It is used to create HTML, XML or other markup formats that are returned to the user via an HTTP request.
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 }}
You convert it before passing it to a template, eg:
>>> import time
>>> date = time.time()
>>> from datetime import datetime
>>> datetime.fromtimestamp(date)
datetime.datetime(2013, 3, 1, 2, 57, 29, 472572)
And optionally use formatting:
>>> format(datetime.fromtimestamp(date), '%Y%m%d')
'20130301'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With