Search code examples
pythondatetime-conversion

Conversion date from tweet in python


I have some problem to convert date in python.

For example you can see on the csv file, at the index 0 for the column "created_date", I have the value "1550740786000".

I would like the convert this date in a more "traditional form"

I tried to use the code below :


Input:

time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(int(pd_testData_filtered["created_at"].iloc[0])))

Output:

51109-10-14 02:13:20

This tweet has been published at 14:29 20/02/2019

Could someone help me?

Best regards.

CSV file


Solution

  • This looks like a Posix timestamp but expressed in milliseconds. So strip the last 3 digits:

    >>> timestamp = 1550740786000
    >>> datetime.datetime.fromtimestamp(timestamp / 1000)
    datetime.datetime(2019, 2, 21, 10, 19, 46)
    

    This isn't exactly the publication time you report, but some hours earlier, and it is plausibly close enough to be worth investigating: the original timestamp of a retweet, maybe?