Avatar

Blogging tips

Avatar

Python, Django and Flask

Avatar

Latest tech posts

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.

strptime

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.
Example:-
datetime.strptime('2019-08-09 01:01:01', "%Y-%m-%d %H:%M:%S")
datetime.datetime(2019, 8, 9, 1, 1, 1)//Result

strftime

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.

Example:-

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