- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个程序来计算两个日期之间的天数,除了一种情况外,它工作正常。如果我想计算两个日期之间的天数,并且结束日期是二月,则天数不正确(正好缺少三天)
示例:
Date 1: 2012,1,1
Date 2: 2012,2,28
Program returns 55 days (should be 58)
我猜想闰日有问题,但我不明白为什么这不会导致任何其他两个日期出现任何错误值,以及为什么正确值和我的程序值之间的差异是 3 天。我的代码示例应该按原样工作,如下所示。如有任何建议,我们将不胜感激。
daysOfMonths = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# Count the number of leap years
def countLeapYears(year, month):
if month <= 2:
year = year - 1
return int(year/4 - year/100 + year/400 )
# Determine the number of days between 0/00/0000 and the two dates and calculate the difference
def daysBetweenDates(year1, month1, day1, year2, month2, day2):
days = 0
n1 = year1 * 365 + day1
for month in range (0, month1):
n1 += daysOfMonths[month]
n1 += countLeapYears(year1, month1)
n2 = year2 * 365 + day2
for month in range (0, month2):
n2 += daysOfMonths[month]
n2 += countLeapYears(year2, month2)
return n2 - n1
def test():
test_cases = [((2012,1,1,2012,2,28), 58),
((2011,6,30,2012,6,30), 366),
((2011,1,1,2012,8,8), 585 ),
((1900,1,1,1999,12,31), 36523)]
for (args, answer) in test_cases:
result = daysBetweenDates(*args)
if result != answer:
print "Test with data:", args, "failed"
else:
print "Test case passed!"
test()
最佳答案
这些行中存在差一错误:
for month in range (0, month1):
...
for month in range (0, month2):
列表在 Python 中是零索引的,但月份在程序中是一索引的。所以正确的代码是:
for month in range (month1 - 1)
...
for month in range (month2 - 1)
关于Python,计算两个日期之间的天数在一种特定情况下返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35104022/
我的目标是获取购买给定产品所需的平均天数。如果 Product_A 在给定时间段内购买了 3 次(“2012-12-01”、“2012-12-05”、“2012-12-10”),那么我们的平均订单间隔
我在计算利息天数时有一个有趣的错误。我每天检查并检查今天是哪一天(1 到 31)。现在我发现了一个问题:10月计数不正常。这意味着 27 号是 26 号,或者 29 号是 28 号。这是一个众所周知的
你好我一直在做一些程序,我的程序是获取一年中过去的天数。现在,当我尝试运行时,输出结果为“4438232”。 例如,如果用户输入 (mm-dd-yy) 3-18-2013,则该年经过的扩孔天数为 77
我看到此问题已针对 Java 得到解答, JavaScript , 和 PHP ,但不是 C#。那么,如何在 C# 中计算两个日期之间的天数? 最佳答案 假设 StartDate 和 EndDate
QQ每天抽超级会员成长值、天数 非必中 手Q打开链接进入活动,下拉页面随便分享一下领取抽奖机会,亲测3点成长值! 活动地址:https://sourl.cn/37CZad 手Q扫码:
是否有任何内置函数可以找出时间戳之间的天数、添加天数或查找时间戳之间的月数?目前,我将日期作为字符串存储在我的文档中。 例如,如果我这样做: return (DATE_TIMESTAMP("2014-
我无法弄清楚如何为以下场景编写公式。我需要根据另一列计算日期,但需要根据原始列的星期几添加天数。 If day of week equals 'Mon/Wed/Fri/Sat' then add 5
我有两个系列的 Pandas 约会时间。我为数据框中的每一行减去它们,并添加一列以获取两个日期时间之间的时间增量。随后我想使用该时间增量来扩展另一个功能。所以我想对那个时间增量进行一些划分。没有骰子。
这个问题已经有答案了: "cannot find symbol: method" but the method is declared (3 个回答) How to add one day to a
我的代码是这样的,但第三行出错 mm/dd/yyyy 我的数据库日期格式如下: SELECT evep year(date='MM/dd/yyyy'), month(da
我有两个 jQuery UI 日期选择器,当它们都选择了日期时,我希望这些日期之间的差异显示在单独的 input[type="text"] 一旦选择了第二个日期。 此外,理想情况下,我希望我的计数减去
我有两列:rental_date 和 actual_retdate。我需要找到 actual_retdate 和 rental_date 之间的天数。 actual_retdate 在某些情况下可以为
运行 MySql 和 Yii 我需要更改当前查找当月记录的关系查询以查找过去 45 天的所有记录。 这是我当前的查询: 'itemCount' => array(self::STAT, 'It
我想创建一个新列,它将显示两个日期之间的时间增量(以天为单位),如以下 pandas 数据框所示: >>> hg[['not inc','date']] not inc
我有: 年份数字(可以是任何年份) 月份数(从一月到十二月) 周数(第 1、2、3、4、最后) 工作日(周日、周一、周二、周三、周四、周五、周六) 我需要获取天数 [从 1 到 ~31] - "YYY
我需要为一些项目倒数 90 天、120 天和 160 天。我将如何编码?我一直在寻找 java 代码,但是在我为 android 开发时会出现错误。 我需要用今天的日期减去 x 天,然后将结果显示在屏
我的要求是计算给定的两个日期之间的天数,不包括星期六和星期日。 示例: Start date - 10/09/15 and End date 18/09/15 Result: 7 日期采用 DD/MM
我想比较我的 Android 应用程序的两个日期,但我遇到了一个非常奇怪的问题。 例如: 如果我将 回到过去 日期设置为 127 天前: this.dateEvent = System.current
如何返回所提供日期的 CumSum 天数? import pandas as pd df = pd.DataFrame({ 'date': ['2019-01-01','2019-01-03'
例如,我可以使用以下代码获取当前日期以显示在 DatePicker 对话框中。 final Calendar c = Calendar.getInstance(); mYear = c
我是一名优秀的程序员,十分优秀!