Python datetime comparison is incorrect in case of compare two time like: 12:00 and 11:59 -
so below example of code:
>>> datetime import datetime >>> future = datetime.strptime('12:00', '%i:%m') >>> past = datetime.strptime('11:59', '%i:%m') >>> future < past >>> true # expected false, because '12:00' > '11:59' >>> past_2 = datetime.strptime('11:58', '%i:%m') >>> past < past_2 >>> false
why datetime compare operation returns true instead of false?
%i
hours twelve hour clock. unless supply or pm (%p
), takes choice. 12:00 (i.e. midnight) before 11:59 am.
if use %h
24 hour clock, in 12:00 noon instead of midnight.
https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
Comments
Post a Comment