简体   繁体   English

如何在python中将日期/时间字符串转换为经过的时间

[英]How to convert date/time string to an elapsed time in python

Using python, how does one convert a date/time retrieved from AWS to an elapsed time from present?使用 python,如何将从 AWS 检索到的日期/时间转换为当前经过的时间? Depending on the actual interval, I would like to display it as years ago, days ago, hours ago, or minutes ago.根据实际间隔,我想将其显示为几年前、几天前、几小时前或几分钟前。 This "function" will be used in several scripts used to produce monthly audit data.此“函数”将用于生成每月审计数据的多个脚本中。

|-------------------------|---------------------|-------------------------|
|Group Name               |Group Id             |Create Date              |
|=========================================================================|
|AWS-Consultants          |AGPAYPADVGQQQBEUBC6SN|2020-06-08 15:28:20+00:00|
|-------------------------|---------------------|-------------------------|

I believe the easiest way that can be achieved is using the timeago library:我相信可以实现的最简单方法是使用 timeago 库:
https://pypi.org/project/timeago/ https://pypi.org/project/timeago/

You can compute a timedelta:您可以计算时间增量:

from datetime import datetime,timezone
create = datetime.strptime('2020-06-08 15:28:20+00:00','%Y-%m-%d %H:%M:%S%z')
print(datetime.now(timezone.utc) - create)

Output:输出:

417 days, 1:55:45.579676

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM