how to get current time in python

You can use the datetime module in Python to get the current time. Here's how:

main.py
import datetime

current_time = datetime.datetime.now()

print("Current Time =", current_time)
95 chars
6 lines

This will output the current date and time in the following format: "YYYY-MM-DD HH:MM:SS.ssssss".

You can also format the output to display just the time by using the strftime() method:

main.py
current_time = datetime.datetime.now()
time_str = current_time.strftime("%H:%M:%S")

print("Current Time =", time_str)
119 chars
5 lines

This will output the current time in the following format: "HH:MM:SS".

related categories

gistlibby LogSnag