-3

If the given date pattern is below,

2004-7-7

How can I convert this into below pattern,

2004-07-07

Using datetime.datetime or some built in function? I don't want to use the replace or string formatting. Does python provide anything (builtin function) to help in such scenario?

Thanks

qurat
  • 439
  • 1
  • 7
  • 14
  • Possible duplicate of [How to convert a time to a string](https://stackoverflow.com/questions/4855406/how-to-convert-a-time-to-a-string) – code11 Sep 13 '18 at 17:33
  • try [strftime](https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior) – Lucas Wieloch Sep 13 '18 at 17:34
  • Possible duplicate of [Parse date string and change format](https://stackoverflow.com/questions/2265357/parse-date-string-and-change-format) – jpp Sep 13 '18 at 17:34

1 Answers1

0

Use datetime module with strptime and strftime:

from datetime import datetime

s = datetime.strftime(datetime.strptime('2004-7-7', '%Y-%m-%d'), '%Y-%m-%d')
print(s)

# 2004-07-07
Austin
  • 25,759
  • 4
  • 25
  • 48