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"""
1from datetime import date
2//today is an object so you can get the day, month and year by
3//treating it as an object. eg: today.day, today.month, today.year
4today = date.today()
5print("Today's date:", today)
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---------------------------------------------------------
2import datetime
3
4currentDateTime = datetime.datetime.now()
5print(currentDateTime)
6---------------------------------------------------------
7OR
8---------------------------------------------------------
9from datetime import datetime
10
11currentDateTime = datetime.now()
12print(currentDateTime)
13---------------------------------------------------------
14OR
15---------------------------------------------------------
16import datetime
17
18print(datetime.datetime.now())
19---------------------------------------------------------
20OR
21---------------------------------------------------------
22from datetime import datetime
23
24print(datetime.now())
25---------------------------------------------------------