Owais Arshad
Owais Arshad

Reputation: 313

Converting Tweet Time Stamps to DD/MM/YY format

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?

Upvotes: 0

Views: 82

Answers (2)

Simas Joneliunas
Simas Joneliunas

Reputation: 3136

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')

Upvotes: 2

Matthew Story
Matthew Story

Reputation: 3783

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.

Upvotes: 2

Related Questions