开发者

DateTime Python

How to convert "12/17/2010 4:12:12 PM" to a datetime object?

For eg, If it was like "2007-03-04T21:08:12Z", I would have done

dd =datetime.strptime( "2007-03-04T21:08:12Z", "%Y-%m-%dT%H:%M:%SZ" 开发者_JAVA百科)

but for time with AM/PM is there any direct way of doing?


From the strptime(3) man page:

   %I     The hour on a 12-hour clock (1-12).

 ...

   %p     The locale’s equivalent of AM or PM.  (Note: there may be none.)


you can use below to handle "AM" or "PM" in a date

%I refers 12-hour format

%H refers 24-hours format

t = "12/17/2010 4:12:12 PM" res = datetime.datetime.strptime(t, "%m/%d/%Y %I:%M:%S %p") print res datetime.datetime(2010, 12, 17, 16, 12, 12)

%p - refers am/AM/pm/PM.


d1= '12/17/2010 4:12:12 PM'

fmt = '%m/%d/%Y %H:%M:%S %p'

d2=datetime.datetime.strptime(d1, fmt)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜