【问题标题】:(Help Needed) Time between two dates w/o Dateti(需要帮助)没有日期时间的两个日期之间的时间
【发布时间】:2016-12-01 09:39:34
【问题描述】:

我必须在 Python 中以天为单位计算两个日期之间的时间,但没有 datetime 模块。

我设法在没有datetime 的情况下解析日期:

def date(error):
    global sdate
    if error:
        print(sdate," isn't a valable date ! ")
    sdate = input('Date : ')
    try:
        ldate = sdate.split('.')
        ldate = [ int(x) for x in ldate]
        day,month,year = ldate
    except ValueError:
        date(True)
    if year%4==0 and year%100!=0 or year%400==0:
        bis = True
    else:
        bis = False
    if month not in range(1,13):
        date(True)
    if month not in(1,3,5,7,8,10,12):
        mmax = 31
    elif month in(4,6,9,11):
        mmax = 30
    elif month == 2 and bis == True:
        mmax = 29
    elif month == 2 and bis == False:
        mmax = 28
    if day not in range(1,mmax+1):
        date(True)
date(None)
print(ldate)

但不知道如何获取日期之间的时间。

最简单的方法是什么?

谢谢, 布拉索

PS:这根本不是家庭作业,我需要它用于个人项目,在那里我使用任何太容易生活的模块;)

【问题讨论】:

  • 如果你避开datetime,这既不容易也不是Pythonic。
  • 尝试提供一些示例代码。到目前为止,您尝试过什么?
  • 我会尽快修改问题
  • 请格式化您的代码并发布。没有适当的缩进。

标签: python date datetime module days


【解决方案1】:

要检查两个日期之间的差异,您需要做的远不止这些。

我会给你伪代码来做到这一点,编写你自己的程序。

• get the dates
• validate the dates 
    -> (ex. It should be false for feb 29 2007)
• calculate total number of days of that particular year for both dates 
    -> (ex 1 jan 2015 = 01 and 28 feb 2016 = 59)
• calculate the year difference
• calculate number of leap years between two days (excluding both end)
    -> (2004 and 2008 leap year is 0)
• calculate the difference by
  diff(number of days) + (year difference * 365) + number of leap years

【讨论】:

  • 写程序太长了。
  • 我不是要你写程序,只是为了精确;)
  • 然后,使用我给出的伪代码。它应该运作良好。
  • 你能给我举个例子吗?我不确定“不包括两端”
  • (2004 and 2008 leap year is 0) 你不应该考虑 2004 年或 2008 年。而对于 2004 年和 2009 年,闰年是 1
猜你喜欢
  • 2012-06-13
  • 1970-01-01
  • 2021-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-10
  • 2018-04-30
  • 1970-01-01
相关资源
最近更新 更多