How to understand strptime vs. strftime

前端 未结 3 1902
夕颜
夕颜 2021-01-31 02:11

What\'s the difference between strptime and strftime? I see that strptime is a method in the DateTime class, and strfti

3条回答
  •  醉梦人生
    2021-01-31 02:55

    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 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

提交回复
热议问题