Parse date string and change format

后端 未结 9 821
孤街浪徒
孤街浪徒 2020-11-22 00:58

I have a date string with the format \'Mon Feb 15 2010\'. I want to change the format to \'15/02/2010\'. How can I do this?

9条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 01:48

    You can install the dateutil library. Its parse function can figure out what format a string is in without having to specify the format like you do with datetime.strptime.

    from dateutil.parser import parse
    dt = parse('Mon Feb 15 2010')
    print(dt)
    # datetime.datetime(2010, 2, 15, 0, 0)
    print(dt.strftime('%d/%m/%Y'))
    # 15/02/2010
    

提交回复
热议问题