如何在Python中计算年和月的差异?

时间:2013-02-25 14:45:24

标签: python python-2.7 datediff

我有一个脚本可以从datetime获取日期,以及来自whois服务的日期:

import time
from datetime import datetime
import whois
#date
url='testurl.com'
now = datetime.now()
date1 = datetime.strftime(nowstr,'%Y-%m')
#whois
domain = whois.whois(url)
datestr = domain.creation_date
date2 = datetime.strftime(datestr, '%Y-%m')

当我计算差异时:

diff = datetime.strptime(date1,'%Y-%m') - datetime.strptime(date2,'%Y-%m')
print diff

它给我的输出如下:

5451 days, 0:00:00

如何以年和月的格式(在我的情况下,14y 11m)获得输出?

1 个答案:

答案 0 :(得分:1)

拿一个look at this post。我相信它能回答你的问题。它显示了使用标准和非标准python库。