0

I would like to only print '%d/%m/%Y, %H:%M', but when I say:

print(loan_due.strftime('%d/%m/%Y, %H:%M'))

I'm getting an error saying AttributeError: 'str' object has no attribute 'strftime'.

I tried many ways but it's still not working for me. loan_due I have in my json file and this how it looks:

{"customer": [{"loan_due": "22/12/2017, 19:02:39:4536"}]}
martineau
  • 119,623
  • 25
  • 170
  • 301
sadiq ali
  • 67
  • 1
  • 8
  • 2
    `my_dict['customer'][0]['loan_due']` is just a string. You need it to be a `datetime` object to be able to call `stftime` on it. Look into `strptime` to convert a string to a datetime. See [the docs](https://docs.python.org/3/library/datetime.html) – roganjosh Dec 22 '17 at 21:32
  • 1
    You would also need to `import datetime` for that to work. You haven't provided a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of what you're actually trying to do. – roganjosh Dec 22 '17 at 21:35

1 Answers1

0

You can't convert it to a string, because it's actually already a string. If you want to change the way in which the string is formatted, try converting it to a datetime object first.

The easiest way to do this is using datetime.strptime. You can review the documentation for it here: https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

You can then use your datetime.strftime to convert this to whatever format you see fit.

Daniel Long
  • 1,162
  • 14
  • 30