Datetime strptime in python

后端 未结 5 549
小鲜肉
小鲜肉 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:17

    As astutely noted in the comments, you are parsing to a datetime object using the format you specified.

    strptime(...) is String Parse Time. You have specified the format for how the string should be interpreted to initialize a Datetime object, but that format is only utilized for initialization. By default, when you go to print that datetime object, you are getting the representation of str(DatetimeObjectInstance) (in your case, str(d)).

    If you want a different format, you should use String Format Time (strftime(...))

提交回复
热议问题