Search code examples
pythonpython-datetime

Python identify timestamp format from string


If I have a string of timestamp, is there some python library that can identify the format of timestamp and give me a datetime object? Example of a string is:

Sat, 16 Jan 2016 07:50:17 GMT

Can you also give an example of how it can be done?


Solution

  • Use dateutil. You can install it with pip.

    from dateutil.parser import parse
    dt = parse('Sat, 16 Jan 2016 07:50:17 GMT')
    print(dt)
    print(type(dt))
    

    Output

    2016-01-16 07:50:17+00:00
    <type 'datetime.datetime'>