3

I am using the FullCalendar plugin for JQuery.

This plugin passes a unix timestamp to a url (both start and end times), which is then used to query the event objects in the database and pull out events which fall within the range of start-end.

However, I keep getting a validation error because the times in my database are stored as datetimes and the value I am comparing them to is a UNIX timestamp.

How can I compare these two dates when running a query?

def fetch_events(request):
    start = request.GET.get('start')
    end = request.GET.get('end')
    e = Event.objects.filter(start__gte=start, end__lte=end).values('id','title','start','end')
    data = simplejson.dumps(list(e), cls=DjangoJSONEncoder)
    return HttpResponse(data)
Ben S
  • 1,407
  • 1
  • 13
  • 27
  • this is a duplicate question, see this http://stackoverflow.com/questions/3682748/converting-unix-timestamp-string-to-readable-date-in-python – Matti Lyra Aug 22 '11 at 20:45

1 Answers1

6

Try making a datetime object from a timestamp by using the datetime.datetime.fromtimestamp function. Here's a quick demonstration:

In [7]: import datetime

In [8]: datetime.datetime.fromtimestamp(2344353453)
Out[8]: datetime.datetime(2044, 4, 15, 19, 17, 33)
Drekembe
  • 2,620
  • 2
  • 16
  • 13
  • 1
    Absolutely excellent! Had to also convert the get string number to an integer: `start = datetime.datetime.fromtimestamp(int(start))` – Ben S Aug 22 '11 at 21:04