strptime
- The meaning of is
str
+parse
+time,
to convert the string into a time format
Example 1
from datetime import datetime now = datetime.now() print(now) # 2021-07-18 12:04:53.157482
-
DateTime
is a module. TheDateTime
module also contains aDateTime
class -
To obtain a specified date and time, to specify a certain date and time.
Example 2
from datetime import datetime dt = datetime(2020, 6, 22, 12, 20) # datetime created with the specified date and time print(dt)
strftime: datetime to string
Example 3
from datetime import datetime now = datetime.now() year = now.strftime("%Y") print("year:", year) month = now.strftime("%m") print("month:", month) day = now.strftime("%d") print("day:", day) time = now.strftime("%H:%M:%S") print("time:", time) date_time = now.strftime("%Y/%m/%d, %H:%M:%S") print("date and time:",date_time)
The Above Code Outputs the
year: 2021 month: 07 day: 18 time: 12:11:13 date and time: 2021/07/18, 12:11:13