-
# -*- coding: utf-8 -*-
-
import time
-
import datetime
-
-
def time2age(atime):
-
"""
-
input:atime 一个时间戳(毫秒)
-
return:age 年龄(跟当前时间相比)
-
"""
-
# 1.将时间转化为一个datetime对象
-
atime_date_obj = datetime.datetime.fromtimestamp(atime/1000)
-
# 2.将datetime对象转化为一个元祖,便于后续比较
-
atime_tuple = (atime_date_obj.year,atime_date_obj.month,atime_date_obj.day)
-
# 3.当前日期
-
now_date_obj = datetime.datetime.now()
-
now_tuple = (now_date_obj.year,now_date_obj.month,now_date_obj.day)
-
# 4.计算差值
-
age = now_tuple[0] - atime_tuple[0] + (now_tuple[1:] > atime_tuple[1:]) - 1
-
-
return age
-
-
-
if __name__ == "__main__":
-
t = time.time() - 1000000000
-
print(time2age(t * 1000))