KingFu
KingFu

Reputation: 1358

Django: 'unicode' object has no attribute 'year'. What format date/time do I need?

I'm getting 'unicode' object has no attribute 'year' error from the timesince filter in django. It was working fine previously with strings in this format: "2013-06-20". However I've now updated the string to include an time element: "2013-06-20T11:20:05.499274" causing this error.

What format do I need to give it to handle the time element? Or do I need to do some extra processing in the view?

Upvotes: 0

Views: 3033

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174624

The timesince filter only accepts dates, not date and time combinations. It displays the time difference from the current time (or optionally, a passed in date to compare with).

In your view, parse that string to its date component only. If it you already have it as a datetime object, simply call .date() on it to get the date part.

If you have it as a string:

fmt = "%Y-%m-%dT%H:%M:%S.%f"
date_only = datetime.strptime("2013-06-20T11:20:05.499274", fmt).date()

Upvotes: 1

Related Questions