1from datetime import datetime
2now = datetime.now()
3print (now.strftime("%Y-%m-%d %H:%M:%S"))
4
5
6Output: 2020-06-19 10:34:45
1import datetime
2
3currentDT = datetime.datetime.now()
4print(str(currentDT))
5
6# prints XXXX-XX-XX XX:XX:XX.XXXXXX
7# or
8
9import datetime
10
11currentDT = datetime.datetime.now()
12
13print ("Current Year is: %d" % currentDT.year)
14print ("Current Month is: %d" % currentDT.month)
15print ("Current Day is: %d" % currentDT.day)
16print ("Current Hour is: %d" % currentDT.hour)
17print ("Current Minute is: %d" % currentDT.minute)
18print ("Current Second is: %d" % currentDT.second)
19print ("Current Microsecond is: %d" % currentDT.microsecond)
20# prints
21"""
22Current Year is: XXXX
23Current Month is: XX
24Current Day is: XX
25Current Hour is: XX
26Current Minute is: XX
27Current Second is: XX
28Current Microsecond is: XXXXXX
29"""
1
2from datetime import datetime
3
4now = datetime.now()
5
6current_time = now.strftime("%H:%M:%S")
7print("Current Time =", current_time)
8
1"""Imports the datetime package from the Python library"""
2from datetime import datetime
3
4"""Sets the variable now to the current date and time"""
5now = datetime.now()
6
7"""The variable current_time contains the string values of the current time"""
8current_time = now.strftime("%H:%M:%S")
9print("Current Time =", current_time)
1
2from datetime import datetime
3
4now = datetime.now().time() # time object
5
6print("now =", now)
7print("type(now) =", type(now))
1import time
2
3t = time.localtime()
4current_time = time.strftime("%H:%M:%S", t)
5print(current_time)
6