【问题标题】:How can I calculate difference of year and month in Python? [duplicate]如何在 Python 中计算年月差? [复制]
【发布时间】:2013-02-25 14:45:23
【问题描述】:

我有一个脚本可以从 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)?

【问题讨论】:

    标签: python python-2.7 datediff


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      相关资源
      最近更新 更多