Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #96685

Re: datetime.datetime.today()

Subject Re: datetime.datetime.today()
From Michiel Overtoom <motoom@xs4all.nl>
Date 2015-09-16 17:01 +0200
References <CANc-5Uwa0q0pgfBGfHLWyFzQYzS0cBhna+ARyPN9RJ2j8ar7Bw@mail.gmail.com> <CAGuvt93ys+iOr7unUOXP0rFRkE_AGo5FWEn2RkGn9NyfjfH7gA@mail.gmail.com> <CANc-5Uz7bON+aXG1vmOpuVAqga9v8h+tLBjvWtDbWo6hF3OXTw@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.646.1442415769.8327.python-list@python.org> (permalink)

Show all headers | View raw


This bit me once. I was comparing a date to a datetime, both representing the same day, so I expected them to be the same, but I was wrong. What I should have done was extracting the date of the datetime with the .date() function, and only then compare it to the other date:

>>> import datetime
>>> a = datetime.datetime.today()
>>> a
datetime.datetime(2015, 9, 16, 16, 57, 45, 150069)
>>> b = datetime.date.today()
>>> a == b
False
>>> a.date()
datetime.date(2015, 9, 16)
>>> a.date() == b
True

Greetings,

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: datetime.datetime.today() Michiel Overtoom <motoom@xs4all.nl> - 2015-09-16 17:01 +0200

csiph-web