I'm using datetime
module. I was told that 24:00:00 is a ValueError
cause the hour is range from 00 to 23. So what time is the end of
the day in 24hr clock?
---------Edited----------
Up to now I prefer Gord's answer. Though Igor has a very practical one.
My question is what's the very last time of datetime
module. Since it has a resolution of microseconds. Gord's answer is the most accurate.
datetime
objects have a resolution of microseconds, so the last possible time value for a given day would be 23:59:59.999999. That is
hours: 23
minutes: 59
seconds: 59
microseconds: 999999
So, if you have a datetime
, you can set it to the end of the day using the replace method:
import datetime
now_datetime = datetime.datetime.now()
end_of_day_datetime = now_datetime.replace(hour=23, minute=59, second=59, microsecond=999999)
Get the beginning of the next day and substract 1 microsecond:
import datetime
source_datetime = datetime.datetime.now()
eod = datetime.datetime(
year=source_datetime.year,
month=source_datetime.month,
day=source_datetime.day
) + datetime.timedelta(days=1, microseconds=-1)
print(eod) # 2016-01-04 23:59:59.999999
There is no 24:00:00 in practice. After 23:59:59 there is 0:00:00
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