给定输入日期,字符串找到Age in Days

时间:2014-02-03 14:30:05

标签: python string date datetime formatting

我有一个计算年龄的程序。我正在考虑使用它来帮助我计算我的工资天数。 我希望它要求日期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没有引用。

2 个答案:

答案 0 :(得分:2)

您可以使用以下内容计算日期(日期之间的天数):

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

答案 1 :(得分:0)

计算机科学问题简介:年龄

  • 我应该告诉大家限制:无进口
  • 我为此道歉,这是我提出的第一个问题,不知道如何正确地构建它们。

  • 我的解决方案如下没有任何导入:
  • 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年&amp; 7个月后:)。

    我已经解决了这个问题,但是忘记将其作为社区的答案发布。 如果其他人可能觉得这对我有用,我就把它留在这里。

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