Get the current date and time

You can get the current date and time using Python. However, you will need to import a module called datetime. To do this, we need to use a single statement – import datetime. Here’s the code to get the current date and time in Python:

import datetime
currentDate = datetime.datetime.now()
print(currentDate)

The code above produces the following output when executed:

>>> 
2022-01-29 02:28:07.475789
>>>

To get only the current date, you can use the following code:

import datetime
currentDate = datetime.datetime.now().date()
print(currentDate)

Here is the output:

>>> 
2022-01-29
>>>

To obtain the current time, you can use the time() command:

import datetime
currentDate = datetime.datetime.now().time()
print(currentDate)

The output:

>>> 
02:29:16.675685
>>>
Geek University 2022