简体   繁体   中英

How to convert datetime.timedelta to number? Field 'days' expected a number but got datetime.timedelta

My code:

data.days=(form.cleaned_data['checkout'] - form.cleaned_data['checkin'])

The error message:

Field 'days' expected a number but got datetime.timedelta

timedelta has a days property you can use:

data.days = (form.cleaned_data['checkout'] - form.cleaned_data['checkin']).days
# Here -------------------------------------------------------------------^

datetime.timedelta is an object instantiated, besides the standard constructor way, as a result of the difference between two datetime objects. It represents a time duration expressed in days, seconds and microseconds.

timedelta also provides a set of properties to access those values, namely:

timedelta.days
timedelta.seconds
timedelta.microseconds

the first one being the one you're searching for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM