-1

I’m looking to change the date format from 02/01/2021 to 2/1/21. What’s the possible solution?

I tried slicing:

start = 20 
df["year"]= df["year"].astype(str)
df["year"]= df["year"].str.slice(start)

I know it’s wrong, but I don’t have any idea how to do this.

1 Answers1

1
from datetime import datetime

date = datetime.strptime("02/01/2021", "%d/%m/%Y")

print(date.strftime("%d/%#m/%y"))

Output:

02/1/21

PS: you can use - instead of # if you are on Linux.

Aymen
  • 841
  • 1
  • 12
  • 21