-4
now = datetime.strptime(str(col[4].text), '%d.%m.%Y, %H:%M:%S')

print(type(now))

But the output says that the type of the now variable is: class 'datetime.datetime'

How to convert it to a custom format?

2
  • datetime.strptime's docs clearly indicate a datetime will be returned. What did you expect? Commented Aug 7, 2019 at 18:56
  • What are you asking for? What you are trying to do worked and that's a problem? Commented Aug 7, 2019 at 19:00

1 Answer 1

1

Datetime objects have a default format when they are printed. The format section of strptime defineds the string format that the datetime STARTS as. Once it is converted to a datetime, it uses the standard representation defined by __repr__ and __str__.

To convert these values to strings in the format that you want, you can use strftime:

now = datetime.strptime(str(col[4].text), '%d.%m.%Y, %H:%M:%S')
now = now.strftime('%d.%m.%Y, %H:%M:%S')

The docs for datetime objects states that print(datetime_object) will return the ISO format representation of datetime_object. ISO format is the YYYY-MM-DD format that you are seeing when printing the datetime.

Read more here: https://docs.python.org/3.2/library/datetime.html

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.