Aucune description

age.py 606B

    import datetime def stamp2age(stamp): """ input:stamp 一个时间戳(毫秒) return:age 年龄(跟当前时间相比) """ # 将时间转化为一个datetime对象 stamp_dt = datetime.datetime.fromtimestamp(stamp / 1000) # 将datetime对象转化为一个元祖,便于后续比较 stamp_tuple = (stamp_dt.year, stamp_dt.month, stamp_dt.day) # 当前日期 now_dt = datetime.datetime.now() now_tuple = (now_dt.year, now_dt.month, now_dt.day) # 计算差值 age = now_tuple[0] - stamp_tuple[0] + (now_tuple[1:] > stamp_tuple[1:]) - 1 return age