0

I want to convert this time 2022-02-24T01:44:22 to 02/24/2022

since_date = 2022-02-24T01:44:22
print (since_date.strftime("%m/%d/%Y"))

Error:

AttributeError: 'str' object has no attribute 'strftime'
Aaron88
  • 177
  • 1
  • 8
  • 19
  • `since_date = 2022-02-24T01:44:22` That is not valid python code. Please post your real code. – John Gordon Mar 05 '22 at 01:55
  • `since_date` is already a string (according to the error), so you will need to [`datetime.strptime`](https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime) it into a datetime object before you can format it back into a string. – Gaberocksall Mar 05 '22 at 01:56
  • Does this answer your question? [Parse date string and change format](https://stackoverflow.com/questions/2265357/parse-date-string-and-change-format) – Iain Shelvington Mar 05 '22 at 01:57
  • My code: since_date = (datetime.datetime.now(datetime.timezone.utc)- datetime.timedelta(20)).strftime("%Y-%m-%dT%H:%M:%S") I want to convert that output to desired output posted in the thread – Aaron88 Mar 05 '22 at 02:02
  • It's hard to read code in comments. Please update the question with the real code. – John Gordon Mar 05 '22 at 02:09
  • @Aaron88 Did the answer work for you? – Sadra Mar 05 '22 at 23:58

1 Answers1

0

This should work for your case:

from datetime import datetime 

date = datetime.strptime('2022-02-24T01:44:22', '%Y-%m-%dT%I:%M:%S').strftime("%m/%d/%y")

print(date)

check this link for other types of formatting.

Sadra
  • 2,480
  • 2
  • 20
  • 32