如何在Python中将rfc822时间戳转换为人类可读的格式?

2024-05-16 10:08:44 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有人知道有一个Python模块可以在Python中将rfc822时间戳转换成人类可读的格式(就像Twitter那样)?在

我找到了parsedatetime,它似乎正好相反。在


Tags: 模块格式时间twitter人类中将rfc822parsedatetime
3条回答

datetime.strptime将把时间戳转换为日期时间对象,您可以用datetime.strftime格式化该对象

http://docs.python.org/library/datetime.html#strftime-strptime-behavior

在python中,可以使用rfc822模块。此模块提供parsedate方法。在

Attempts to parse a date according to the rules in RFC 2822.

但是,此模块已被弃用。在

Deprecated since version 2.3: The email package should be used in preference to the rfc822 module. This module is present only to maintain backward compatibility, and has been removed in 3.0.

根据这个注释,最好使用email.utils模块中的parsedate方法。在

email.utils.parsedate(date)

编辑:

示例代码:

^{pr2}$

输出(目前):

31 days, 2 hours ago

相关问题 更多 >