【问题标题】:Human datetime diffs with PendulumPendulum 的人类日期时间差异
【发布时间】:2017-12-15 15:46:19
【问题描述】:

我最近偶然发现了这个很棒的 pendulum“datetimes made easy”库。

它有一个非常好的功能,可以显示日期时间之间的“类人”差异:

In [1]: import pendulum

In [2]: now = pendulum.now()

In [3]: future = now.add(years=10)

In [4]: future.diff_for_humans()
Out[4]: '10 years from now'

但是,是否有可能让它适用于更复杂的差异——比如“年”和“周”?

In [5]: future = now.add(years=10, weeks=5)

In [6]: future.diff_for_humans()
Out[6]: '10 years from now'

我希望它输出10 years and 5 weeks from now

【问题讨论】:

    标签: python datetime pendulum


    【解决方案1】:

    来自 Pendulum 模块自述文件:

    now = pendulum.now()
    future = now.add(years=10, weeks=5)
    delta = future - now
    delta.in_words()
    >>>'10 years 1 month 4 days'
    

    https://github.com/sdispater/pendulum

    【讨论】:

    • 很好的发现,我想知道为什么diff_for_humans()in_words() 之间存在这种差异。谢谢!
    • 您可以随时查看实现。我链接了模块源。
    • 好的,read the source, Luke :)
    • 如果需要'from now',它不会回答问题。当需要本地化时,仅手动添加不是一种选择。
    • 我刚刚为此开了一个问题:github.com/sdispater/pendulum/issues/169.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    相关资源
    最近更新 更多