I have a bunch of twitter timestamps in the following format, stored in a csv file e.g. "Wed Oct 04 17:31:00 +0000 2017"
. How can I convert these to a format like DD/MM/YY
? Would this require the dateutil
module?
Asked
Active
Viewed 62 times
0

Simas Joneliunas
- 2,890
- 20
- 28
- 35

Owais Arshad
- 303
- 4
- 18
-
1Possible duplicate of [Python strptime() and timezones?](https://stackoverflow.com/questions/3305413/python-strptime-and-timezones) – T4rk1n Jul 04 '18 at 01:56
2 Answers
2
You can do it with python's datetime module:
from datetime import datetime
datetime_object = datetime.strptime('Wed Oct 04 17:31:00 +0000 2017', '%a %b %d %H:%M:%S %z %Y')
converted_date = datetime_object.strftime('%d/%m/%y')

Matthew Story
- 3,573
- 15
- 26

Simas Joneliunas
- 2,890
- 20
- 28
- 35
2
While you can certainly use the datetime.strptime
method to accomplish this, I generally have found dateutil
to be far easier to deal with for timestamps like these:
>>> from dateutil import parser
>>> parser.parse("Wed Oct 04 17:31:00 +0000 2017").strftime("%d/%m/%Y")
'04/10/2017'
The pro to using this method is that you don't have to strictly define the input format that you expect, and it works with a whole bunch of standard formats. The con compared to strptime
is that it's not quite as explicit as strptime. Depending on your needs one or the other might be better or worse.

Matthew Story
- 3,573
- 15
- 26