Search code examples
pythontwitter

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?


Solution

  • 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.