Saturday, February 23, 2019

Python: Strptime


strptime

String to datetime object = strptime
datetime object to other formats = strftime
Jun 1 2005 1:33PM is equals to %b %d %Y %I:%M%p
%b  - Month as locale’s abbreviated name(Jun)
%d   - Day of the month as a zero-padded decimal number(1)
%Y   - Year with century as a decimal number(2015)
%I   - Hour (12-hour clock) as a zero-padded decimal number(01)
%M  - Minute as a zero-padded decimal number(33)
%p   - Locale’s equivalent of either AM or PM(PM)
 Use slicing to remove unwanted characters if necessary.

Example :
>>> import time
>>> time.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p')
time.struct_time(tm_year=2005, tm_mon=6, tm_mday=1,
                 tm_hour=13, tm_min=33, tm_sec=0,
                 tm_wday=2, tm_yday=152, tm_isdst=-1)

No comments:

Post a Comment