datetime.datetime.today()

Nick Sarbicki nick.a.sarbicki at gmail.com
Wed Sep 16 09:55:02 EDT 2015


> This surprised me today:
>
> >>> import datetime
> >>> datetime.datetime.today(), datetime.datetime.now()
> (datetime.datetime(2015, 9, 16, 8, 44, 7, 723560), datetime.datetime(2015,
> 9, 16, 8, 44, 7, 723577))
>
> I naively expected today() to always return a datetime.date object. Oh
> well, bug in my code has been squashed.
>

Just in the case you didn't figure it out:

 >>> datetime.datetime.today()
datetime.datetime(2015, 9, 16, 14, 50, 47, 700828)
>>> datetime.date.today()
datetime.date(2015, 9, 16)

You were accessing the today method of the datetime property which
inevitably returns a datetime object. Getting today from the date object
will return an actual date.

Now and today are very, very, similar - but now may be more accurate and
gives flexibility with timezones as per
https://docs.python.org/3/library/datetime.html#datetime.datetime.now

- Nick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150916/d24d0bd3/attachment.html>


More information about the Python-list mailing list