【问题标题】:Are these two formatting date methods really equivalent?这两种格式化日期方法真的等效吗?
【发布时间】:2017-01-12 09:49:37
【问题描述】:

看来:

time.strftime("%Y-%m-%d %H:%M:%S")
#2017-01-12 10:46:57

datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
#2017-01-12 10:46:57

是等价的。当我在这里阅读许多问题/答案、教程或文档时,其中一些使用前者,一些使用后者。

它们是 100% 等效的吗?什么时候选择其中一个?

PS:the latter 似乎等价于datetime.date.fromtimestamp(time.time()),但这是否等价于time.strftime(...)

【问题讨论】:

    标签: python datetime time


    【解决方案1】:

    前者似乎更快?和more here

    import time
    import datetime
    %timeit time.strftime("%Y-%m-%d %H:%M:%S")
    %timeit datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
    

    输出:

    100000 loops, best of 3: 4.02 µs per loop
    100000 loops, best of 3: 10.8 µs per loop
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      • 2018-03-07
      • 2021-01-11
      相关资源
      最近更新 更多