-1

I get start_date like this: start_date1 = datetime.datetime.utcnow().replace(tzinfo=utc)

and pass end_date as a function argument like this:

a(datetime.datetime.utcnow().replace(tzinfo=utc))

They both are stored in database under start_date and end_date coloum name.

What I am trying to do is subtract end_date and start_date to get duration:

  start_date = [i.start_date for i in b ]
  end_date = [i.end_date for i in b ]

I would use this to subtract those two dates: t = [i - j for i, j in zip(y, x)]

However the problem is start_date and end_date are returned in unicodes. I can't convert them into datetime format again. What should I do?

pynovice
  • 7,424
  • 25
  • 69
  • 109
  • If you want to show the difference at templates, you can use `timesince` tag. https://docs.djangoproject.com/en/dev/ref/templates/builtins/#timesince – Babu Mar 18 '13 at 07:04
  • Python has extensive, timezone-aware [timedelta](http://docs.python.org/2/library/datetime.html#timedelta-objects) features which are well-documented. – class stacker Mar 18 '13 at 07:11

1 Answers1

0

The result of substracting one datetime object from another in python is a timedelta object. A datetime object is defined as a period of time from epoch (00:00:00 on 1 Jan 1970). Subtracting one datetime from another cannot return a new datetime object (since it can't be relative to Epoch), what you'll get back is just period of time. That's a timedelta.

There's lots of detail in the docs.

Aidan Ewen
  • 13,049
  • 8
  • 63
  • 88
  • I know that. But the start_date and end_date are returned in unicode from databases which can't be subtracted. – pynovice Mar 18 '13 at 07:16
  • Then you need to convert back to datetime objects before you do your calculation. You'll want to use the strptime function - http://docs.python.org/2/library/time.html#time.strptime – Aidan Ewen Mar 18 '13 at 07:22