简体   繁体   English

将datetime值转换为整数

[英]converting datetime value to integer

I am trying to make the passed_days variable an integer, so that I can add the values. 我试图使pass_days变量成为一个整数,以便我可以添加值。 So if passed_days = 1 , passed_days should equal an integer not a datetime. 因此,如果passed_days = 1 ,则passed_days应该等于整数而不是datetime。

I tried converting the passed_days var to as a int like int(passed_days) . 我尝试将passed_days var转换为类似int(passed_days) This is the current code I have now: 这是我现在的代码:

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

I need the output to be something like...184 + 2 我需要输出像... 184 + 2

To get the days represented by a timedelta , use the days property like: 要获得timedelta代表的timedelta ,请使用days属性,例如:

passed_days.days

Testcode: Testcode:

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)

Results: 结果:

<class 'int'>
0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM