+ 3
What is the difference between strptime and strftime
Python datetime
2 Antworten
+ 5
Both are used for date time operations. strptime is for parsing date time input, strftime is for formatting date time output. You canfind more in the official documentation also some samples:
https://docs.python.org/3/library/datetime.html
+ 4
The strptime() method creates a datetime object from a given string.
example:
date_string = "1 August, 2019"
date_object = datetime.strptime(date_string, "%d %B, %Y")
print("date_object =", date_object)
The strftime() method returns a string representing date and time using date, time or datetime object.
example:
now = datetime.now()
year = now.strftime("%Y")
print("year:", year)