Hellnar
Hellnar

Reputation:

django Datefield to Unix timestamp

In a model I have a such field: mydate = models.DateField()

now a javascript graph function requires unix timestamp such as "1196550000000", how can I return the unix timestamp of my mydate input.

Thanks

Upvotes: 38

Views: 42037

Answers (6)

Oleg Shleif
Oleg Shleif

Reputation: 740

A very simple way that I did not find in the answers

import time
timestamp = int(time.time())

Upvotes: 0

serg
serg

Reputation: 111365

Another option:

import time
from django.utils import timezone

naive_date = timezone.make_naive(mydate, timezone.get_current_timezone())
print int(time.mktime(naive_date.timetuple()))

Upvotes: 1

Benjamin Wohlwend
Benjamin Wohlwend

Reputation: 31888

edit: please check the second answer, it has a much better solution

In python code, you can do this to convert a date or datetime to the Unix Epoch

import time
epoch = int(time.mktime(mydate.timetuple())*1000)

This doesn't work in a Django template though, so you need a custom filter, e.g:

import time

from django import template

register = template.Library()

@register.filter
def epoch(value):
    try:
        return int(time.mktime(value.timetuple())*1000)
    except AttributeError:
        return ''

Upvotes: 28

John Paulett
John Paulett

Reputation: 15834

I know another answer was accepted a while ago, but this question appears high on Google's search results, so I will add another answer.

If you are working at the template level, you can use the U parameter to the date filter, e.g.:

{{ mydate|date:"U" }}

Note that it will be based upon the TIMEZONE in your settings.py.

Upvotes: 109

Mike Fogel
Mike Fogel

Reputation: 3197

And if you're not in the template layer, you can still use the same underlying django utils. Ex:

from django.utils.dateformat import format
print format(mymodel.mydatefield, 'U')

Upvotes: 70

Meredith L. Patterson
Meredith L. Patterson

Reputation: 4919

In your views.py, you can convert the value of mydate to seconds since the Unix epoch as follows:

seconds = time.mktime(mydate.timetuple())

Then pass it in the dictionary you use as an argument to render_to_response() (or whatever you're using to render your view), and in your template, stick {{seconds}} into a hidden field, which you can then pull out of the DOM to pass to your javascript graph function.

Note that a DateField maps to the Python object datetime.date, and as such, its timetuple will have its hours, minutes and seconds fields set to 0. If that's not fine-grained enough for you, you'll need to change mydate to a DateTimeField and it'll be a datetime.datetime. You can still use mydate.timetuple() if you do this.

Also, I'm assuming you're using local time. If you're using UTC time, you want calendar.gmtime() rather than time.mktime() and mydate.utctimetuple() rather than mydate.timetuple(), but utctimetuple() is only a valid method for datetime.datetime objects. See the datetime docs (also time and calendar) for more fiddly details.

EDIT: fiddly details such as the fact that mktime() returns a float, which piquadrat remembered and I didn't. The custom-filter approach is also a good one. Voting that one up.

Upvotes: 6

Related Questions