I have a python script which I need to compare two dates. I have a list dates as time.struct_time objects which I need to compare to a few datetime.date objects.
How do I convert the datetime.date objects into a time.struct_time objects? Or can I just use them as is for comparison?
Try using date.timetuple()
. From the Python docs:
Return a
time.struct_time
such as returned bytime.localtime()
. The hours, minutes and seconds are 0, and the DST flag is -1.d.timetuple()
is equivalent totime.struct_time((d.year, d.month, d.day, 0, 0, 0, d.weekday(), yday, -1))
, whereyday = d.toordinal() - date(d.year, 1, 1).toordinal() + 1
is the day number within the current year starting with 1 for January 1st.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With