【问题标题】:Calculate days until your next birthday in python在 python 中计算直到你下一个生日的天数
【发布时间】:2019-01-08 05:14:04
【问题描述】:

在上面的代码中,我想计算直到下一个生日的天数,但输出错误。 它应该是什么: 我的生日:2002 年 2 月 20 日 => 距离我生日还有 203 天(今天是 2018 年 7 月 31 日) 它实际上是什么: 输入:2002 年 2 月 20 日 => 179 天

我的代码:

import datetime


def get_user_birthday():
    year = int(input('When is your birthday? [YY] '))
    month = int(input('When is your birthday? [MM] '))
    day = int(input('When is your birthday? [DD] '))

    birthday = datetime.datetime(year,month,day)
    return birthday


def calculate_dates(original_date, now):
    date1 = now
    date2 = datetime.datetime(now.year, original_date.month, original_date.day)
    delta = date2 - date1
    days = delta.total_seconds() / 60 /60 /24

    return days


def show_info(self):
    pass



bd = get_user_birthday()
now = datetime.datetime.now()
c = calculate_dates(bd,now)
print(c)

【问题讨论】:

    标签: python python-3.x datetime logic


    【解决方案1】:

    几个问题:

    1. 年份必须指定为一个完整的整数,即 2002,而不是 02(或 2)。
    2. 您需要检查您今年的生日是否已过。

    以下是解决这两个问题的解决方案。根据您输入的 2002 年 2 月 20 日和今天的日期 2018 年 7 月 31 日,您的下一个生日是 203 天后。

    另外,请注意,您可以使用timedelta 对象的days 属性,该属性将向下舍入到203 天并避免小数精度。

    from datetime import datetime
    
    def get_user_birthday():
        year = int(input('When is your birthday? [YY] '))
        month = int(input('When is your birthday? [MM] '))
        day = int(input('When is your birthday? [DD] '))
    
        birthday = datetime(2000+year,month,day)
        return birthday
    
    def calculate_dates(original_date, now):
        delta1 = datetime(now.year, original_date.month, original_date.day)
        delta2 = datetime(now.year+1, original_date.month, original_date.day)
        
        return ((delta1 if delta1 > now else delta2) - now).days
    
    bd = get_user_birthday()
    now = datetime.now()
    c = calculate_dates(bd, now)
    
    print(c)
    
    When is your birthday? [YY] 02
    When is your birthday? [MM] 02
    When is your birthday? [DD] 20
    
    113
    

    【讨论】:

      【解决方案2】:

      想想你的 calculate_dates 函数在做什么。

      您正在过生日,然后查看当前时间与当年的生日相差多远。因此,您正在做的是找出您生日的天数今年,无论它是否已经过去。

      例如,您的生日是 2 月 20 日。您的 date2 将是 2018-2-20 而不是 2019-2-20

      您可以通过检查这一天是否已经过去来解决此问题。

      【讨论】:

        【解决方案3】:

        这应该适用于两种情况(今年或明年的下一个生日) 多年来使用两位数可能总是一个坏主意,尤其是对于生日;)

        def get_days_until_birthday(birthday: date, today: date) -> int:
            this_year = (date(today.year, birthday.month, birthday.day) - today).days
            if this_year >= 0:
                return this_year
        
            next_year = (date(today.year+1, birthday.month, birthday.day) - today).days
            return next_year
        

        【讨论】:

          【解决方案4】:

          这就是我计算生日前几天的方法:

          from datetime import datetime
          
          
          def get_user_birthday():
              year = int(input('When is your birthday? [YY] '))
              month = int(input('When is your birthday? [MM] '))
              day = int(input('When is your birthday? [DD] '))
          
              birthday = datetime(year,month,day)
              return birthday
          
          
          def calculate_dates(birthyday):
              now = datetime.now()
              birthday = datetime(now.year, birthday.month, birthday.day)
              return (birthday - now.today()).days + 1
          
          
          bd = get_user_birthday()
          c = calculate_dates(bd)
          print(c)
          

          【讨论】:

            【解决方案5】:
            from datetime import datetime
            from datetime import date
            import time
            
            today = date.today()
            
            def user_birthday():
                year = int(input('When is your birthday? [YY] '))
                month = int(input('When is your birthday? [MM] '))
                day = int(input('When is your birthday? [DD] '))
            
                birthday = datetime(year,month,day)
                return birthday
            
            def calculate_dates(birthday):
                today == date.fromtimestamp(time.time())
                birthday = date(today.year, birthday.month, birthday.day)
                if birthday < today:
                    birthday = birthday.replace(year=today.year + 1)
                    return birthday
                else:
                    return birthday
                
            bday = user_birthday()
            t = calculate_dates(bday)
            time_to_birthday = abs(t-today)
            days=str(time_to_birthday.days)
            print("Time to Birthday is :" + days + " days")
            

            无论是今年到来还是今年过去,上述程序都会告诉你天数(在这种情况下,它将计算明年)

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-12-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-10-04
              • 2010-11-13
              相关资源
              最近更新 更多