Datetime strptime in python

后端 未结 5 615
小鲜肉
小鲜肉 2021-02-04 08:30

I\'ve tried the following code :

import datetime
d = datetime.datetime.strptime(\"01/27/2012\", \"%m/%d/%Y\")
print(d)

and the output is :

5条回答
  •  青春惊慌失措
    2021-02-04 09:14

    You need to make sure you provide input accordingly

    datetime.strptime(date_string,date_string_format).strftime(convert_to_date_string_format)
    

    To print the date in specified format you need to provide format as below.

    import datetime
    d =datetime.datetime.strptime("01/27/2012","%m/%d/%Y").strftime('%m/%d/%Y')
    print d
    

    Output:

    01/27/2012
    

    >>Demo<<

提交回复
热议问题