pythonpython-2.7datetimerfc2822

Compare date with current date in python


How do I compare a datetime object of this format

"Thu, 15 Jan 2015 06:35:37 GMT" with the current time ?


Solution

  • You can use datetime module to convert from datetime string into datetimetime object. then get different between two datetime objects.

    >>> from datetime import datetime
    >>> a = datetime.strptime("Thu, 15 Jan 2015 06:35:37 GMT", "%a, %d %b %Y %H:%M:%S GMT", )
    >>> b = datetime.strptime("Fri, 16 Jan 2015 07:40:40 GMT", "%a, %d %b %Y %H:%M:%S GMT", )
    >>> c1 = b - a 
    >>> c1
    datetime.timedelta(1, 3903)
    >>> c1.days
    1
    >>> c1.total_seconds()
    90303.0
    >>> c1.seconds
    3903
    >>>