Python datetime.strptime - Converting month in String format to Digit -
i have string contains date in format: full_date = "may.02.1982"
i want use datetime.strptime() display date in digits like: "1982-05-02"
here's tried:
full_date1 = datetime.strptime(full_date, "%y-%m-%d")
when try print this, garbage values built-in-67732
going wrong? strptime() method not accept string values?
your format string wrong, should this:
in [65]: full_date = "may.02.1982" import datetime dt dt.datetime.strptime(full_date, '%b.%d.%y') out[65]: datetime.datetime(1982, 5, 2, 0, 0)
you need call strftime
on datetime object string format desire:
in [67]: dt.datetime.strptime(full_date, '%b.%d.%y').strftime('%y-%m-%d') out[67]: '1982-05-02'
strptime
creating datetime format string, not reformat string datetime string.
so need create datetime object using strptime
, call strftime
create string datetime object.
the datetime format strings can found in docs explanation of strptime
, strftime
Comments
Post a Comment