Python:求时间差(天时分秒格式)

传入一个时间戳,以天时分秒格式打印出时间差

输入一个10位的时间戳,求出时间差

def time_diff(timestamp):
	onlineTime = datetime.datetime.fromtimestamp(timestamp)
	localTime = datetime.datetime.now()
	result = localTime - onlineTime
	hours = int(result.seconds / 3600)
	minutes = int(result.seconds % 3600 / 60)
	seconds = result.seconds%3600%60
	print("当前网络时间:{0}".format(localTime))
	print("上次在线时间:{0}".format(onlineTime))
	print("{0}天{1}时{2}分{3}秒前在线".format(result.days,hours,minutes,seconds))

times = int(time.time())-80
time_diff(times)

运行效果:

当前网络时间:2020-05-27 18:00:03.393412
上次在线时间:2020-05-27 17:58:43
0天0时1分20秒前在线

posted @ 2020-05-27 19:15  往事如云烟都付笑谈中  阅读(2770)  评论(0编辑  收藏  举报