How can this datetime variable be converted to the string equivalent of this format in python? -
i using python 2.7.10
i have datetime variable contains 2015-03-31 21:02:36.452000
. want convert datetime variable string looks 31-mar-2015 21:02:36
.
how can done in python 2.7?
use strptime create datetime object use strftime format way want:
from datetime import datetime s= "2015-05-31 21:02:36.452000" print(datetime.strptime(s,"%y-%m-%d %h:%m:%s.%f").strftime("%d-%b-%y %h:%m:%s")) 31-may-2015 21:05:36
the format string following:
%y year century decimal number. %m month decimal number [01,12]. %d day of month decimal number [01,31]. %h hour (24-hour clock) decimal number [00,23]. %m minute decimal number [00,59]. %s second decimal number [00,61]. %f microsecond decimal number
in strftime use %b is:
%b locale’s abbreviated month name.
obviously ignore microseconds in output string.
if have datetime object call strftime on datetime object:
print(dt.strftime("%d-%b-%y %h:%m:%s"))
Comments
Post a Comment