Beyond
Beyond

Reputation: 139

how to convert unix timestamp to datetime in django's templete?

First: My database is Mysql,and the table has exists,it been used for php,all the time fileds'type is unix_timestamp.

the query result return an unix timestamp string. how to convert the unix timestamp to datetime in django's templete?

second:

about regex,

my code here:

import re
pattern=re.compile("^\d+$")
if pattern.match(50):
  print 1
else:
  print 0

but it show me "TypeError" why ?

Thanks!

My english is very pool~! I'm sorry

Upvotes: 1

Views: 4459

Answers (1)

kubus
kubus

Reputation: 783

Second:

import re
pattern=re.compile(r"^\d+$")
if pattern.match(u"50"):
    print 1
else:
    print 0

First:

I can offer a custom template filter, which converts timestamp to python datetime object:

@register.filter(name='fromunix')
def fromunix(value):
    return datetime.datetime.fromtimestamp(int(value))

Template:

{{ obj.unixtime|fromunix|date:"F" }}}

https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#registering-custom-filters

Upvotes: 8

Related Questions