Question

I have some problem, I use python.

I have 2 var, like

ads = fields.Date('Admission Date', help='Date of admission')
dds = fields.Date('Discharge Date', help='Date of discharge')

I have one var to subtract the date,

los=ads-dds

but, I have some error:

unsupported operand type(s) for -: 'datetime.datetime' and 'datetime.date'

what should I do to get the result of los?

Was it helpful?

Solution

One of those fields is a date object, the other a datetime object. You'll have to decide what you want subtraction to mean.

You could, for example, turn the date object into a datetime object with a fixed time of day, say, midnight:

los = ads - datetime.datetime.combine(dds, datetime.time.min)

datetime.datetime.combine() takes a date and a time object and creates a new datetime object; we use datetime.time.min as an easy short-cut to a time object representing midnight.

Or, you could just turn the datetime object into a date object and then subtract:

los = ads.date() - dds

The datetime.date() method returns just the date component of a datetime object. The result is a datetime.timedelta() object representing the number of days between the two dates.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top