Home > Mobile >  How to format date 20220410 to 2022-04-10 in Python?
How to format date 20220410 to 2022-04-10 in Python?

Time:04-10

I need help formatting the date.

I have a variable date as an integer like this:

date = 20220410 # YYMMDD

Note: This date is not hardcoded. It is dynamic

Desired format:

2022-04-10

I written some algorithms to do this with for loops. But didn't work!

CodePudding user response:

>>> import datetime
>>> datetime.datetime.strptime("20220410", "%Y%m%d").date().strftime("%Y-%m-%d")
'2022-04-10'

CodePudding user response:

import datetime

x = datetime.datetime(2020, 5, 17)

print(x)
  • Related