Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook timestamp to Python DateTime Object

Facebook returns 'created_time' in this format:

2012-07-23T08:52:04+0000

I want to convert this timestamp to a normal Python DateTime object.

like image 949
Nazim Zeeshan Avatar asked Jul 28 '12 11:07

Nazim Zeeshan


People also ask

How do I convert a timestamp to a date in Python?

Converting timestamp to datetime We may use the datetime module's fromtimestamp() method to convert the timestamp back to a datetime object. It returns the POSIX timestamp corresponding to the local date and time, as returned by time. time().

How do I cast a timestamp to a date?

You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.

How do I extract a date from a timestamp in Python?

For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt. date from Pandas in Python.


2 Answers

Have you tried dateutil

It's extremely easy to use

import dateutil.parser as dateparser
dateparser.parse('2012-07-23T08:52:04+0000')

dateutil is very helpful to deal with timezone info, and it can handle lots of time formats.

like image 113
xiaowl Avatar answered Oct 01 '22 00:10

xiaowl


s = "2005-12-06T12:13:14"
from datetime import datetime
from time import strptime
print datetime(*strptime(s, "%Y-%m-%dT%H:%M:%S")[0:6])
like image 34
Danil Speransky Avatar answered Sep 30 '22 23:09

Danil Speransky