Social Widget

Responsive Ads Here

Wednesday, December 11, 2019

Difference between strptime and strftime

strptime is short for "parse time" where strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification.


When your data is a string, and you want to compute the "date-structure", use "strptime" for "string parse time".

strptime means string parser, this will convert a string format to DateTime.
datetime.strptime('2019-08-09 01:01:01', "%Y-%m-%d %H:%M:%S")
datetime.datetime(2019, 8, 9, 1, 1, 1)//Result


When your data is a structure, and want it formatted in a readable way, use "strftime" for "string format time".

strftime means string formatter, this will format a DateTime object to string format.


sample_date=datetime.strptime('2019-08-09 01:01:01', "%Y-%m-%d %H:%M:%S")
datetime.strftime(sample_date, "%Y-%d-%m %H:%M:%S")
'2019-09-08 01:01:01'//Result

No comments:

Post a Comment