python - Django datetime expression -


hello using django , collecting datetime via

start_date = models.datetimefield('start date', blank=true) end_date = models.datetimefield('end date', blank=true) 

i have report start date , report end date.

i wish retrieve time taken report by,

taking end_date , subtracting end date , rounding nearest day

def report_days(self):     return self.end_date - self.start_date 

currently python shell returns this

>>> a.report_days() datetime.timedelta(2, 65020, 613435) 

django represents datetimefields python datetime objects, result of subtraction datetime.timedelta object. number of full days, can do:

delta = self.end_date - self.pub_date return delta.days 

this counts full days, though. round nearest full day, can like:

delta = self.end_date - self.pub_date if delta.seconds / 3600 >= 12:     return delta.days + 1 # round else:     return delta.days # round down 

for more information, see: http://docs.python.org/2/library/datetime.html#timedelta-objects


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -