Python 输出可读性更好的时间文本

写上篇日志时 偶然发现这个 可以输出可读性很好的日期格式

http://pypi.python.org/pypi/py-pretty/1

py-pretty 1

Formats dates, numbers, etc. in a pretty, human readable format.

Downloads ↓

Sample code::

from datetime import datetime, timedelta
now = datetime.now()
hrago = now - timedelta(hours=1)
yesterday = now - timedelta(days=1)
tomorrow = now + timedelta(days=1)
dayafter = now + timedelta(days=2)

import pretty
print pretty.date(now)                      # 'now'
print pretty.date(hrago)                    # 'an hour ago'
print pretty.date(hrago, short=True)        # '1h ago'
print pretty.date(hrago, asdays=True)       # 'today'
print pretty.date(yesterday, short=True)    # 'yest'
print pretty.date(tomorrow)                 # 'tomorrow'
应该会用得到的
收藏起来
posted @ 2012-01-06 23:50  LemonLi  阅读(610)  评论(0编辑  收藏  举报