1
2from datetime import datetime
3
4now = datetime.now() # current date and time
5
6year = now.strftime("%Y")
7print("year:", year)
8
9month = now.strftime("%m")
10print("month:", month)
11
12day = now.strftime("%d")
13print("day:", day)
14
15time = now.strftime("%H:%M:%S")
16print("time:", time)
17
18date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
19print("date and time:",date_time)
20
1
2from datetime import datetime
3
4date_string = "21 June, 2018"
5
6print("date_string =", date_string)
7print("type of date_string =", type(date_string))
8
9date_object = datetime.strptime(date_string, "%d %B, %Y")
10
11print("date_object =", date_object)
12print("type of date_object =", type(date_object))
13