29👍
You could use custom template filters (see https://docs.djangoproject.com/en/dev/howto/custom-template-tags/).
In your case it could like this:
- Create directory ‘templatetags’ in application with view, that renders template.
-
Put into this dir blank file “__init__.py” and “timetags.py” with code:
from django import template import datetime register = template.Library() def print_timestamp(timestamp): try: #assume, that timestamp is given in seconds with decimal point ts = float(timestamp) except ValueError: return None return datetime.datetime.fromtimestamp(ts) register.filter(print_timestamp)
-
In your template, add
{% load timetags %}
-
Use following syntax in template:
{{ timestamp|print_timestamp }}
Where timestamp = 1337453263.939 from your example
This will print timestamp in local date and time format. If you want to customize output,
you can modify print_timestamp in following way:
import time
def print_timestamp(timestamp):
...
#specify format here
return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(ts))
96👍
Note: this answer actually solves the reverse problem
(displaying a timestamp when having a datetime). The correct answer to
OP (who has a timestamp and wants a datetime) is to make a custom template tag or filter, see the accepted answer
for this.
{% now "U" %}
The "U"
is a date format for Unix epoch, and can also be used with built-in date
filter. So, if you have the date in a variable:
{{ value|date:"U" }}
- [Django]-Why can't it find my celery config file?
- [Django]-Django: How to make a form with custom templating?
- [Django]-Django reverse lookup of foreign keys
1👍
Maybe you can use the date filter:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#date
- [Django]-Setting up Django on AWS Elastic Beanstalk: WSGIPath not found
- [Django]-Django subquery with aggregate
- [Django]-ModelForm with OneToOneField in Django
1👍
I don’t think the date filter takes timestamps, so unless I’m overseeing a filter, you could simply create one?
# python
from datetime import datetime
# django
from django import template
register = template.Library()
@register.filter("timestamp")
def timestamp(value):
try:
return datetime.fromtimestamp(value)
except AttributeError, e:
catch errors..
- [Django]-What is the way to ignore/skip some issues from python bandit security issues report?
- [Django]-Django inclusion tag with configurable template
- [Django]-Testing admin.ModelAdmin in django
0👍
Just write a custom filter that converts your timestamp string to a datetime object. Then you can either further process it into something readable (a string representing your time) and return it to your template or just return a python datetime object and use django’s date filter.
So you could have something like this {{ timestamp_string|convert_to_datetime|date:’D d M Y’ }}
More on this subject at: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
- [Django]-Django: How to get language code in template?
- [Django]-How to implement breadcrumbs in a Django template?
- [Django]-Atomic operations in Django?