I'm currently working on a small project, but I got a problem, I don't know how to convert this "2017-12-22T05:00:00+01:00
" time, to an readable time, I would like to get it to this format "%Y-%m-%d %H:%M:%S"
. Without success so far, is there something I can do to archive this?
Asked
Active
Viewed 35 times
0

martineau
- 119,623
- 25
- 170
- 301

shawnfunke
- 53
- 5
3 Answers
1
First you need to parse the input datetime string into a datetime
object. Then convert the datetime
object to the required format. The easiest way to do that is with the third-party dateutil
package. You can install it with pip
.
>>> from dateutil.parser import parse
>>> dt = parse('2017-12-22T05:00:00+01:00')
>>> dt
datetime.datetime(2017, 12, 22, 5, 0, tzinfo=tzoffset(None, 3600))
Converting to a new format can be done with datetime.strftime()
:
>>> dt.strftime('%Y-%m-%d %H:%M:%S')
'2017-12-22 05:00:00'

mhawke
- 84,695
- 9
- 117
- 138
-
Thanks for the fast help! This is what i needed. – shawnfunke Dec 20 '17 at 22:18
0
See the answer to this question: How to print date in a regular format in Python?
It's a very comprehensive answer with links to the documentations

clair3st
- 109
- 5
-
It doesn't seem like an answer to his question, I think he meant string conversion to another string which would be formatted little bit differently. Parsing of first string would still be part of the answer. – Lycopersicum Dec 20 '17 at 21:49
-
0
If it's the same string each time, you could use something like this:
time_string = '2017-12-22T05:00:00+01:00'
year = time_string.split('-')[0]
month = time_string.split('-')[1]
day = time_string.split('-')[2].split('T')[0]
time = time_string.split('T')[1].split(':')
hour = time[0]
minute = time[1]
second = time[2].split('+')[0]
print(second,minute,hour,day,month,year)
(but there's probably a much better way to do it.)

AaronJPung
- 1,105
- 1
- 19
- 35