【问题标题】:converting datetime value to integer将日期时间值转换为整数
【发布时间】:2019-11-21 19:36:45
【问题描述】:

我正在尝试将 pass_days 变量设为整数,以便我可以添加这些值。所以如果passed_days = 1passed_days 应该等于一个整数而不是一个日期时间。

我尝试将passed_days var 转换为像int(passed_days) 这样的int。 这是我现在拥有的当前代码:

start_day = datetime.date(2019, 7, 11)
current_day = datetime.date.today()
passed_days = current_day - start_day
current = 183

if passed_days > datetime.timedelta(days=0):
    current = 183 + passed_days
else:
    current = 183

我需要输出类似于...184 + 2

【问题讨论】:

    标签: python python-3.x datetime int


    【解决方案1】:

    要获取timedelta 表示的日期,请使用days 属性,例如:

    passed_days.days
    

    测试代码:

    import datetime as dt
    
    start_day = dt.date(2019, 7, 11)
    current_day = dt.date.today()
    passed_days = current_day - start_day
    
    print(type(passed_days.days))
    print(passed_days.days)
    

    结果:

    <class 'int'>
    0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-22
      • 2015-05-31
      • 1970-01-01
      • 2013-10-16
      • 2019-02-16
      • 2021-05-16
      • 2019-06-01
      • 1970-01-01
      相关资源
      最近更新 更多