Home > other >  How to set tm_hour python (time module)
How to set tm_hour python (time module)

Time:06-06

I am trying to subtract two dates without the time. When I try to set the hour, min, and second to 0, I get the error AttributeError: readonly attribute. Is there any way to set the hour min and second to 0?

first_date = datetime_object.date().timetuple()
current_date.tm_hour = 0
current_date.tm_min = 0
current_date.tm_sec = 0
print('current_date', current_date, 'tyeeeee', type(current_date))

CodePudding user response:

Simply make both the dates of type: datetime. Then subtract

diff=second_date - first_date
difference_in_days = diff.days

difference_in_days will give you the number of days in int type.

CodePudding user response:

I think you don't need to set hour, minute and second to zero. As they are zero by default, following datetime library documentation:

date.timetuple: Return a time.struct_time such as returned by time.localtime(). The hours, minutes and seconds are 0, and the DST flag is -1. d.timetuple() is equivalent to:

time.struct_time((d.year, d.month, d.day, 0, 0, 0, d.weekday(), yday, -1))

Reference: https://docs.python.org/3/library/datetime.html#date-objects

  • Related