Get Current Time in Python

Get Current Time in Python

  • Python
  • 2 mins read

In this tutorial, you will learn how to get the current time in Python using multiple examples.

Example 1: Get The Current Time using datetime Library in Python

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

Output:

2021-09-24 06:35:59.744700

Example 2: Get Just The Time in Python

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

Output:

06:37:26.055465

Example 3: Get Date and Time Using time Library

from time import gmtime, strftime
cdt = strftime("%Y-%m-%d %H:%M:%S", gmtime())
print(cdt)

Output:

2021-09-24 06:40:21

Example 4: Get the Date and Time of the Current Location in Python

from datetime import datetime
now = datetime.now()

current_time = now.strftime("%H:%M:%S")
current_date = now.strftime("%D")

print("Current Time: ", current_time)
print("Current Date: ", current_date)

Output:

Current Time: 06:43:33
Current Date: 09/24/21

Example 5: Get the Current date and time in Pandas

import pandas as pd
print(pd.datetime.now())
print(pd.datetime.now().date())
print(pd.datetime.now().year)
print(pd.datetime.now().month)
print(pd.datetime.now().day)
print(pd.datetime.now().hour)
print(pd.datetime.now().minute)
print(pd.datetime.now().second)
print(pd.datetime.now().microsecond)

Output:

2021-09-24 06:46:15.536802
2021-09-24
2021
9
24
6
46
15
537867

Reference: SOF-415511

See also: