django Datefield to Unix timestamp

Hellnar picture Hellnar · Aug 11, 2009 · Viewed 37.6k times · Source

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

Answer

John Paulett picture John Paulett · Jul 31, 2010

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.