-2

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

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
the7blank
  • 41
  • 1

4 Answers4

1

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
Jary
  • 81
  • 4
0

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:

user2390182
  • 72,016
  • 6
  • 67
  • 89
0

if you don't want to use any libraries:

date  = 'yyyy-mm-dd'

def reverse_date(date):
    return '-'.join(date.split('-')[::-1])

James
  • 43
  • 4
0

Try with:

OldDate = '2020-02-10'
NewDate = OldDate.dt.strftime('%d/%m/%Y')

It would print the date in the form you want.

Ruggero
  • 427
  • 2
  • 10