I want to change the string 'datetime.datetime(2018, 4, 15, 12, 59)'
into the format '2018-04-15T12:59'
.
I created a function that looks like:
def changeDateFormat(date):
newDate = []
for i in date.split('datetime')[2].split(','):
newDate.append(re.sub('[() ]', '', i))
newStr = newDate[0] + '-0' + newDate[1] + '-' + newDate[2] + 'T' + newDate[3] + ':' + newDate[4]
return newStr
And it works.
However, I also want it to handle the case where if each unit is less than 10, I want to add a '0'
. Right now it just adds a '0'
regardless.
I can do it in a very inefficient way, but I wonder how I can do it in a smart way.