Calculating days between multiple dates

huangapple go评论70阅读模式
英文:

Calculating days between multiple dates

问题

我正在尝试计算不同日期之间的天数、周数或月数。我正在使用这个计算来操作一个数据框并显示随时间变化的图表(条形图、折线图等)。我尝试使用以下代码来实现:

userdate = input("输入开始日期(YYYY-MM-DD)或键入“全部”以获取整个网络历史:")

startyear = userdate[0:4]
startmonth = userdate[5:7]
startday = userdate[8:10]

endyear = userenddate[0:4]
endmonth = userenddate[5:7]
endday = userenddate[8:10]

#年 月 日 格式
barstartdate = datetime.date(int(startyear), int(startmonth), int(startday))
#年 月 日 格式
barenddate = datetime.date(int(endyear), int(endmonth), int(endday))
#时间差
delta = datetime.timedelta(days=1)
#迭代日期范围
while (barstartdate <= barenddate):
    print(barstartdate, end="\n")
    barstartdate += delta

userdate 和 userenddate 是用户输入的日期值,格式为 YYYY-MM-DD。但是,当我运行这段代码时,我收到以下错误消息:

TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to an 'int' object

英文:

I am attempting to calculate the days, weeks, or months between different dates. I am using this calculation to manipulate a dataframe and show graphs over time (bar charts, line graphs, etc). I am attempting the use the following code to do so:

       userdate = input(&quot;Input Start Date Here (YYYY-MM-DD) Or Type All For Entire Networking History: &quot;)

startyear = userdate[0:4]
startmonth = userdate[5:7]
startday = userdate[8:10]

endyear = userenddate[0:4]
endmonth = userenddate[5:7]
endday = userenddate[8:10]

#year month day format
barstartdate = datetime.date(int(startyear),int(startmonth),int(startday))
#year month day format
barenddate = datetime.date(int(endyear),int(endmonth),int(endday))
#delta time
delta = datetime.timedelta(days=1)
#iterate over range of dates
while (barstartdate &lt;= barenddate):
    print(barstartdate, end=&quot;\n&quot;)
    barstartdate += delta

userdate and userenddate are user inputted values for dates in the format YYYY-MM-DD. However, when I run this code, I get the following error message:

TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'int' object

答案1

得分: 0

I Hope this is it cause it is not letting me comment with low reputation.

我希望这是正确的,因为它不允许我以低声望评论。

I think you're importing incorrectly the module with from datetime import datetime instead of import datetime.

我认为你导入模块时出错了,应该使用import datetime而不是from datetime import datetime

So try with this:

所以请尝试使用以下代码:

import datetime

userdate = input("Enter userdate: ")
userenddate = input("Enter userenddate: ")

startyear = userdate[0:4]
startmonth = userdate[5:7]
startday = userdate[8:10]

endyear = userenddate[0:4]
endmonth = userenddate[5:7]
endday = userenddate[8:10]

#year month day format
barstartdate = datetime.date(int(startyear),int(startmonth),int(startday))
#year month day format
barenddate = datetime.date(int(endyear),int(endmonth),int(endday))
#delta time
delta = datetime.timedelta(days=1)
#iterate over range of dates
while (barstartdate <= barenddate):
print(barstartdate, end="\n")
barstartdate += delta

最后,你可能要检查@dakdad的字符串格式化方法:

import datetime

userdate = input("Enter userdate: ")
date_obj = datetime.datetime.strptime(userdate, '%Y-%m-%d')
print(date_obj)

Output:

输出:

Enter userdate: 1993-05-06
1993-05-06 00:00:00

英文:

I Hope this is it cause it is not letting me comment with low reputation.

I think you're importing incorrectly the module with
from datetime import datetime instead of import datetime

So try with this:

import datetime

userdate = input(&quot;Enter userdate: &quot;)
userenddate = input(&quot;Enter userenddate: &quot;)


startyear = userdate[0:4]
startmonth = userdate[5:7]
startday = userdate[8:10]

endyear = userenddate[0:4]
endmonth = userenddate[5:7]
endday = userenddate[8:10]

#year month day format
barstartdate = datetime.date(int(startyear),int(startmonth),int(startday))
#year month day format
barenddate = datetime.date(int(endyear),int(endmonth),int(endday))
#delta time
delta = datetime.timedelta(days=1)
#iterate over range of dates
while (barstartdate &lt;= barenddate):
    print(barstartdate, end=&quot;\n&quot;)
    barstartdate += delta

Output:

Enter userdate: 1993-05-05
Enter userenddate: 1993-05-10
1993-05-05
1993-05-06
1993-05-07
1993-05-08
1993-05-09
1993-05-10

Finally you should probably check @dakdad method to format strings:

import datetime

userdate = input(&quot;Enter userdate: &quot;)
date_obj = datetime.datetime.strptime(userdate, &#39;%Y-%m-%d&#39;)
print(date_obj)

Output:

Enter userdate: 1993-05-06
1993-05-06 00:00:00

huangapple
  • 本文由 发表于 2023年6月15日 08:00:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定