Python Forum

Full Version: datetime.datetime.now
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i want to import the datetime.datetime.now function, only.  how can i do that?  i still don't understand the package module system.
It's a method, belonging to the datetime type, which belongs to the module. From what I can tell, methods can't be imported.
ok so i have to do:

import datetime
now=datatime.datatime.now
Or:
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2017, 1, 10, 10, 37, 51, 289957)

>>> from datetime import datetime as dt
>>> dt.now()
datetime.datetime(2017, 1, 10, 10, 38, 0, 770687)
Pendulum the best date library for Python.
>>> from pendulum import now
>>> now()
<Pendulum [2017-01-10T11:02:07.852108+01:00]>

>>> now = now('Europe/Paris')
>>> now
<Pendulum [2017-01-10T11:02:24.693479+01:00]>
>>> now.in_timezone('America/Toronto')
<Pendulum [2017-01-10T05:02:24.693479-05:00]>

>>> now.is_weekday()
True
>>> now = now.add(days=4)
>>> now.is_weekday()
False
>>> if now.is_weekend():
...     print('Party!')
...     
Party!
Je vous remercie beaucoup!