Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
datetime.datetime.now
#1
i want to import the datetime.datetime.now function, only.  how can i do that?  i still don't understand the package module system.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
It's a method, belonging to the datetime type, which belongs to the module. From what I can tell, methods can't be imported.
Reply
#3
ok so i have to do:

import datetime
now=datatime.datatime.now
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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!
Reply
#5
Je vous remercie beaucoup!
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why does datetime.strptime() not work in this situation Monomial 2 1,024 Apr-08-2025, 06:36 PM
Last Post: snippsat
Question [SOLVED] [datetime.strptime] ValueError: time data 'foo' does not match format 'bar' Winfried 1 1,514 Jan-02-2025, 02:09 AM
Last Post: lyly19
  Formatting DateTime string and and converting it from AM/PM to 24 hours tester_V 2 1,286 Jun-08-2024, 05:16 PM
Last Post: tester_V
  datetime module question jacksfrustration 9 4,472 Jan-12-2024, 04:54 AM
Last Post: deanhystad
  subtract 2 datetime string jss 4 4,589 Oct-19-2023, 02:42 PM
Last Post: Larz60+
  discrepancy with datetime with timezone XJia 3 3,676 Sep-03-2023, 02:58 PM
Last Post: deanhystad
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 4,352 Dec-17-2022, 12:24 AM
Last Post: snippsat
  Trying to delete rows above a specific datetime value cubangt 19 22,549 May-09-2022, 08:57 PM
Last Post: deanhystad
  Problem with datetime [SOLVED] AlphaInc 3 9,824 Apr-22-2022, 01:33 PM
Last Post: snippsat
  How to get evenly-spaced datetime tick labels regardless of x-values of data points? Mark17 4 9,880 Apr-04-2022, 07:10 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020