You can use datetime module to get the current time in Python as shown in the below examples –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from datetime import datetime ## Get current time print("current time:",datetime.now()) ## Get current year print("current year:",datetime.now().year) ## Get current month print("current month:",datetime.now().month) ## Get current day print("current day:",datetime.now().day) ## Get current hour print("current hour:",datetime.now().hour) ## Get current minute print("current minute:",datetime.now().minute) ## Format datetime print("Format time:",datetime.now().strftime("%m/%d/%Y %H:%M:%S")) |