gokublack gokublack - 4 months ago 6
Python Question

How do you add : to a time formatted like this 203045 in python?

I've been trying to get this time formatted value from 203045 to 20:40:45 in python. I clearly have no clue where to start. Any help will be appreciated!
Thanks!

Answer

Use strptime and strftime functions from datetime, the former constructs a datetime object from string and the latter format datetime object to string with specific format:

from datetime import datetime
datetime.strptime("203045", "%H%M%S").strftime("%H:%M:%S")
# '20:30:45'
Comments