python中的gmail样式日期格式化 [英] gmail style date formatting in python

查看:89
本文介绍了python中的gmail样式日期格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用python中的strftime格式化日期,但现在我想以当前时间的格式显示
date。

I can format date with strftime in python, but now I want to show date in format relative to current time.

即如果日期在今天附近,请及时显示。

i.e. if date is near today, show in time.

如果是在一周内,显示为昨天等。

if it is within week, show as yesterday etc.

如果在月份内显示为上周或月份。

if it is within month just show as last week or month.

如果它比显示为实际值更旧。

if it is older than that show as actual value.

我可以做很多的ifs,但有什么更简单的方法来做到这一点吗?或图书馆?

I can do it with lots of ifs but is there any simpler way to do this? or library?

推荐答案

是图书馆

文档

[python]
>>> from relativeDates import *
>>> import time
>>> x = time.time()-1000
>>> getRelativeTime(x)
'17 minutes ago'
>>> x-=12345
>>> getRelativeTime(x)
'3 hours ago'
>>> x+=543211
>>> getRelativeTime(x)
'in 6 days'
>>> getRelativeTime(x,accuracy=2)
'in 6 days 3 hours'
>>> x-=987661
>>> getRelativeTime(x,accuracy=2)
'5 days 7 hours ago'
>>> getRelativeTime(x,accuracy=2,alternative_past="long long ago")
'long long ago'
>>> getRelativeTimeStr("07/15/06 1823")
'in 4 days'
>>> getRelativeTimeStr("07/10/06 1823")
'7 hours ago'
>>> getRelativeTimeStr("07/10/06 1823",accuracy=2)
'7 hours 30 mins ago'
[/python]

这篇关于python中的gmail样式日期格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆