Python time to age

后端 未结 6 2435
野的像风
野的像风 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 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.
    

提交回复
热议问题