Python time to age

后端 未结 6 2373
野的像风
野的像风 2020-12-20 03:23

I\'m trying to convert a date string into an age.

The string is like: \"Mon, 17 Nov 2008 01:45:32 +0200\" and I need to work out how many days old it is.

I h

相关标签:
6条回答
  • 2020-12-20 03:50

    In Python, datetime objects natively support subtraction:

    from datetime import datetime
    age = datetime.now() - datetime.strptime(...)
    print age.days
    

    The result is a timedelta object.

    0 讨论(0)
  • 2020-12-20 03:57

    Since Python 3.2, datetime.strptime() returns an aware datetime object if %z directive is provided:

    #!/usr/bin/env python3
    from datetime import datetime, timezone, timedelta
    
    s = "Mon, 17 Nov 2008 01:45:32 +0200"
    birthday = datetime.strptime(s, '%a, %d %b %Y %H:%M:%S %z')
    age = (datetime.now(timezone.utc) - birthday) / timedelta(1) # age in days
    print("%.0f" % age)
    

    On older Python versions the correct version of @Tony Meyer's answer could be used:

    #!/usr/bin/env python
    import time
    from email.utils import parsedate_tz, mktime_tz
    
    s = "Mon, 17 Nov 2008 01:45:32 +0200"
    ts = mktime_tz(parsedate_tz(s))   # seconds since Epoch
    age = (time.time() - ts) / 86400  # age in days
    print("%.0f" % age)
    

    Both code examples produce the same result.

    0 讨论(0)
  • 2020-12-20 03:59

    You need to use the module datetime and the object datetime.timedelta

    from datetime import datetime
    
    t1 = datetime.strptime("Mon, 17 Nov 2008 01:45:32 +0200","%a, %d %b %Y %H:%M:%S +0200")
    t2 = datetime.now()
    
    tdelta = t2 - t1 # actually a datetime.timedelta object
    print tdelta.days
    
    0 讨论(0)
  • 2020-12-20 04:03

    Thanks guys, I ended up with the following:

    def getAge( d ):
        """ Calculate age from date """
        delta = datetime.now() - datetime.strptime(d, "%a, %d %b %Y %H:%M:%S +0200")
        return delta.days + delta.seconds / 86400.0 # divide secs into days
    

    Giving:

    >>> getAge("Mon, 17 Nov 2008 01:45:32 +0200")
    78.801319444444445
    
    0 讨论(0)
  • 2020-12-20 04:04
    from datetime import datetime, timedelta
    datetime.now()
    datetime.datetime(2009, 2, 3, 15, 17, 35, 156000)
    datetime.now() - datetime(1984, 6, 29 )
    datetime.timedelta(8985, 55091, 206000)
    datetime.now() - datetime(1984, 6, 29 )
    datetime.timedelta(8985, 55094, 198000) # my age...
    

    timedelta(days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])

    0 讨论(0)
  • 2020-12-20 04:09

    If you don't want to use datetime (e.g. if your Python is old and you don't have the module), you can just use the time module.

    s = "Mon, 17 Nov 2008 01:45:32 +0200"
    import time
    import email.utils # Using email.utils means we can handle the timezone.
    t = email.utils.parsedate_tz(s) # Gets the time.mktime 9-tuple, plus tz
    d = time.time() - time.mktime(t[:9]) + t[9] # Gives the difference in seconds.
    
    0 讨论(0)
提交回复
热议问题