Fusecube
Fusecube

Reputation: 99

Convert datetime in python

How do i convert this datetime using python?

2017-10-16T08:27:16+0000

I tried to use strptime but getting ValueError: time data '2017-10-16T08:27:16+0000' does not match format 'The %d %B %Y at. %H:%M'

import datetime
datetime.datetime.strptime("2017-10-16T08:27:16+0000", "The %d %B %Y at. %H:%M")

'''
I want my output to look like this
The 16 october 2017 at. 08:27
'''

Upvotes: 0

Views: 98

Answers (2)

Martin Schmelzer
Martin Schmelzer

Reputation: 23909

First parse the string correctly, then print it in the desired format:

import datetime
date = datetime.datetime.strptime("2017-10-16T08:27:16+0000", "%Y-%m-%dT%H:%M:%S%z")
print(date.strftime("The %d %B %Y at. %H:%M"))

Upvotes: 2

Gwendal Grelier
Gwendal Grelier

Reputation: 536

https://blogs.harvard.edu/rprasad/2011/09/21/python-string-to-a-datetime-object/ You have to first strip your date using strptime() and then rebuild it using strftime()

import datetime
time = "2017-10-16T08:27:16+0000"
stripedTime = datetime.datetime.strptime(time, '%Y-%m-%dT%I:%M:%S%z')

rebuildTime = stripedTime.strftime('The %d %B %Y at. %H:%M')
print(rebuildTime)

Upvotes: 1

Related Questions