What is the difference between strptime and strftime | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What is the difference between strptime and strftime

Python datetime

2nd Aug 2019, 4:48 PM
Bright Odohofre
Bright  Odohofre - avatar
2 Answers
+ 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
2nd Aug 2019, 5:00 PM
Lothar
Lothar - avatar
+ 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)
2nd Aug 2019, 4:58 PM
Michael
Michael - avatar