kpr cse
kpr cse

Reputation: 5

convert date format from mm/dd/yyyy to yyy/mm/dd in python

I have a date format of mm/dd/yyyy I need to convert into yyyy/mm/dd

 published_date = self.clean(published_dateformat).split('at')[0]
 new_published_date = datetime.date.strftime(published_date,"%Y/%m/%d")

Upvotes: 0

Views: 295

Answers (2)

Shafiqa Iqbal
Shafiqa Iqbal

Reputation: 178

you can do this like this.

from datetime import datetime
published_dateformat = "7/11/2019 at 7.09pm"
published_date = (published_dateformat).split('at')[0][:-1]
print(datetime.strptime(published_date,'%d/%m/%Y').strftime("%Y/%m/%d"))

Upvotes: 1

Lucas Wieloch
Lucas Wieloch

Reputation: 818

try this:

from datetime import datetime
published_dateformat = "7/11/2019 at 7.09pm"
published_date = published_date = (published_dateformat).split('at')[0][:-1]
published_date = datetime.strptime(published_date,"%d/%m/%Y")
new_published_date = published_date.strftime("%Y/%m/%d")

With this, new_published_date will have value of '2019/11/07'

Upvotes: 0

Related Questions