【问题标题】:Given input dates as strings find Age in Days给定输入日期作为字符串查找以天为单位的年龄
【发布时间】:2014-02-27 02:12:23
【问题描述】:

我有一个以天为单位计算年龄的程序。我正在考虑用它来帮助我计算我的发薪日。 我希望它询问日期 1,然后询问日期 2,这也是当前日期。 但是,我希望它更漂亮,甚至不要求日期 2。 我想在运行此代码时使用(time.strftime('%Y,%-m,%-d')) 作为第二组日期的输入。 我已经构建了用于计算我的 3 个工作的总净收入的代码。

y1,m1,d1 = 1994,4,1 #Birth_Date
y2,m2,d2 = 2014,1,1 #Current_Date
print daysBetweenDates(y1,m1,d1,y2,m2,d2)

我的问题是,如果我做了y2,m2,d2 = (time.strftime('%Y,%-m,%-d')) 它会在结尾加上引号打印出来所以 --> '2014,2,3' 然后一切都崩溃了。我一直在到处寻找解决方案 每个人都在谈论日期时间的东西,但我的问题是返回

datetime.strptime('2014-12-04', '%Y-%m-%d').date()
datetime.date(2014, 2, 3)

不仅是日期,还有datetime.date

然后我尝试了

s = datetime.strptime('2014-12-04', '%Y-%m-%d').date()
s = string [16:-1] # 

删除所有多余的东西,但这也不起作用。

请帮我解决这个问题。 我想要y2,m2,d2 = current date 不带引号。

【问题讨论】:

    标签: python string date datetime formatting


    【解决方案1】:

    您可以使用以下方法计算年龄(日期之间的天数):

    import datetime
    start_date = datetime.datetime.strptime('1975-10-03', '%Y-%m-%d').date()
    end_date = datetime.datetime.strptime('2014-12-05', '%Y-%m-%d').date()
    day_between_dates = (end_date - start_date).days
    >> 14307
    

    【讨论】:

    • 我将投票赞成并接受它作为答案,因为这最终是解决这个问题的最佳方法。这很容易重现,以后可以随时使用。我的解决方案虽然符合原始问题的约束,但以后没有用。我也知道那里还有很多其他技术问题,因为日期时间的东西是一场噩梦。
    【解决方案2】:

    计算机科学问题简介:以天为单位的年龄

  • 我应该告诉大家以下限制:禁止进口
  • 对此我深表歉意,这是我提出的第一个问题,但不知道如何正确提出它们。

  • 我在下面没有任何导入的解决方案:
  • def isLeapYear(year):
        if year % 4 == 0:
            if year % 100 != 0:
                return True
            else:
                if year % 400 == 0:
                    return True
        return False
    
    def daysInMonth(year,month):
        if month == 1:
            return 31
        if month == 2:
            if isLeapYear(year) == True:
                return 29
            return 28
        if month == 3:
            return 31
        if month == 4:
            return 30
        if month == 5:
            return 31
        if month == 6:
            return 30
        if month == 7:
            return 31
        if month == 8:
            return 31
        if month == 9:
            return 30
        if month == 10:
            return 31
        if month == 11:
            return 30
        if month == 12:
            return 31
    
    
    def nextDay(year, month, day):
        """Simple version: assume every month has 30 days"""
    
        if day < daysInMonth(year,month):
            return year, month, day + 1
        else:
            if month == 12:
                return year + 1, 1, 1
            else:
                return year, month + 1, 1
    
    def dateIsAfter(year1, month1, day1, year2, month2, day2):
        """Returns True if year1-month1-day1 is after year2-month2-day2.  Otherwise, returns False."""
        if year1 > year2:
            return True
        if year1 == year2:
            if month1 > month2:
                return True
            if month1 == month2:
                return day1 > day2
        return False        
    
    def daysBetweenDates(year1, month1, day1, year2, month2, day2):
        """Returns the number of days between year1/month1/day1
           and year2/month2/day2. Assumes inputs are valid dates
           in Gregorian calendar."""
        # program defensively! Add an assertion if the input is not valid!
        assert dateIsAfter(year2, month2, day2, year1, month1, day1)
        days = 0
        while dateIsAfter(year2, month2, day2, year1, month1, day1):
            days += 1
            (year1, month1, day1) = nextDay(year1, month1, day1)
        return (days) 
    
    y1,m1,d1 = 1994,7,14 #Birth_Date
    y2,m2,d2 = 2014,1,1 #Current_Date
    print daysBetweenDates(y1,m1,d1,y2,m2,d2)
    

    我的回答:1 年 7 个月后 :)。

    我已经解决了,只是忘记在社区中发布它作为答案。 如果其他人觉得这对学习有用,我将把它留在这里。

    P.S 但是,感谢@user3277225 的回答。这是一个非常优雅和简单的解决方案,我可以在以后需要时使用。

    【讨论】:

      猜你喜欢
      • 2019-10-11
      • 2012-08-12
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      • 2012-11-12
      • 2018-01-17
      • 1970-01-01
      • 2010-12-07
      相关资源
      最近更新 更多