subtract dates with time module

barronmo barronmo at
Thu Mar 27 00:47:45 CET 2008

I'm trying to get the difference in dates using the time module rather
than datetime because I need to use strptime() to convert a date and
then find out how many weeks and days until that date.  I'm a beginner
so any help would be appreciated.  Here is the code:

def OBweeks(ptID):
     qry = 'SELECT short_des FROM problems WHERE patient_ID = %s;'  %
     results = EMR_utilities.getAllData(qry)
     for items in results:
          r ='\d\d\d\d-\d\d-\d\d', items)
          if r:
               d = time.strptime(, "%Y-%m-%d')  -
               weeks, days  = divmod(d.days, 7)
               return '%s %s/7 weeks'  % (weeks, days)

This isn't working.  I'm getting "unsupported operand type for -:
'time.struct_time' and 'time.struct_time" error.

Thanks, Mike

More information about the Python-list mailing list