I need to create a function that receives string yyyy-mm-dd
and it returns the string as dd-mm-yyyy
.
For example,
Sample input: 2020-02-10
Expected output: 10-02-2020
I need to create a function that receives string yyyy-mm-dd
and it returns the string as dd-mm-yyyy
.
For example,
Sample input: 2020-02-10
Expected output: 10-02-2020
maybe you can do that
import datetime
date = '2020-02-10'
date = datetime.datetime.strptime(date,"%Y-%m-%d").strftime("%d-%m-%Y")
print(date)
>>> 10-02-2020
You don't even need datetime
utils. Some simple str
manipulation will do:
def convert(datestr):
return "-".join(reversed(datestr.split("-")))
convert("2020-02-10")
# '10-02-2020'
Some documentation:
if you don't want to use any libraries:
date = 'yyyy-mm-dd'
def reverse_date(date):
return '-'.join(date.split('-')[::-1])
Try with:
OldDate = '2020-02-10'
NewDate = OldDate.dt.strftime('%d/%m/%Y')
It would print the date in the form you want.