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
1from datetime import datetime as d
2import time as t
3from os import system as clear
4cls = lambda: clear("clear")
5
6prev = d.now()
7prev = prev.strftime("%Y-%m-%d %H:%M:%S")
8
9while True:
10 time = d.now()
11 time = time.strftime("%Y-%m-%d %H:%M:%S")
12 print(time)
13 if not time == prev:
14 prev = time
15 t.sleep(1)
16 cls()
17