1

I have a string output from another program that shows the date as

16/05/03 # (YY/MM/DD)

and I wish to change it to

03/05/16 #(DD/MM/YY)

and here is how the date is supplied

(date = info[4].replace('"', '')

i have tried

dates = str(date)[::-1]

but that gave me an output of

 40/50/61

not quite what I wanted

any ideas using a minimal code as possible?

2 Answers 2

4
>>> '/'.join('16/05/03'.split('/')[::-1])
'03/05/16'

or

>>> '/'.join(reversed('16/05/03'.split('/')))
'03/05/16'

or using datetime library:

>> from datetime import datetime
>>> datetime.strftime(datetime.strptime('16/05/03', '%y/%m/%d'), '%d/%m/%y')
'03/05/16'
0
2

Using datetime give you alot more control with changing the format to suite what you want.

import datetime
d = datetime.strptime('16/05/03', '%y/%m/%d')
print d.strftime('%d/%m/%y')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.