Geek University

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:

>>> 
1997-06-29 14:56:08.800197
>>>

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:

>>> 
1997-06-29
>>>

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

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

The output:

>>> 
14:57:47.801347
>>>

Subscribe to our Newsletter

Like us on Facebook

Share on Twitter

top
Developed by Signum Soft