zoukankan      html  css  js  c++  java
  • 计算一个人从出生到现在活了多少天

    要求:给出一个人的出生日期,计算出该人到今天已经活过的天数,出生的那一天算一天,现在这一刻也算一天

    思路:假定这个人是19900324现在时间为20140310

    1.计算1990年到2014年这24年中的天数 days = (2014 - 1990) * 365,每一年按平年算

    2.计算1990年到2014年有有n个闰年 days = days + n

    3.计算0324到0310的天数temp_days days = days + temp_days

    这中间还有一些细节需要考虑,下面是用python实习的代码,试了两个日期没怎么调试

    Caculate.py

     1 import datetime
     2 import pdb
     3 
     4 #定义计算一个人从出生到现在活的天数类
     5 class Caculate:
     6     #每个月天数 2月算的28天
     7     __monthDaysArray = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
     8     __days = 0
     9 
    10      #定义计算函数
    11     def caculateDays(self):
    12         #获取用户的出生日期
    13         bir_date = input("请输入你的出生日期,格式为19900806")
    14         bir_year = int(bir_date[0:4])
    15         bir_month = int(bir_date[4:6])
    16         bir_day = int(bir_date[6:8])
    17         #获取当前系统年月日
    18         now =datetime.datetime.now()
    19         cur_year = now.year
    20         cur_month = now.month
    21         cur_day = now.day
    22         #计算如19900806-20140806之间的天数
    23         self.__days = (cur_year - bir_year) * 365
    24         #计算1990-2014之间闰年次数 出生月份小月2月从1990年开始计算
    25         if bir_month < 2:
    26             for tempYear in range(bir_year, cur_year - 1, 1):
    27                 if self.isReap(tempYear):
    28                     self.__days += 1
    29         else:
    30             for tempYear in range(bir_year + 1, cur_year, 1):
    31                 if self.isReap(tempYear):
    32                     self.__days += 1
    33         temp_days = 0
    34         if cur_month < bir_month:
    35             temp_days = self.__monthDaysArray[cur_month] - cur_day
    36             for temp_month in range(cur_month + 1, bir_month, 1):
    37                 temp_days += self.__monthDaysArray[temp_month]
    38             if cur_month <= 2 and bir_month > 2:
    39                 if self.isReap(cur_year):
    40                     temp_days += 1
    41             self.__days -= temp_days
    42         if cur_month > bir_month:
    43             self.__days += temp_days
    44         self.__days += 1
    45 
    46         return  self.__days
    47 
    48     #判断是否为闰年
    49     def isReap(self, year):
    50         if year % 100 == 0 and year % 4 == 0:
    51             return True
    52         elif year % 100 != 0 and year % 4 == 0:
    53             return True
    54         else:
    55             return False

    mainF.py

     1 import Caculate
     2 #测试类
     3 def main():
     4     caculate = Caculate.Caculate()
     5     days = caculate.caculateDays()
     6     print("你已经活了", days)
     7 
     8 
     9 if __name__ == '__main__':
    10     main()

    中间还有很多异常没有处理

  • 相关阅读:
    thinkphp在模型中自动完成session赋值
    highcharts实例教程二:结合php与mysql生成饼图
    程序员应该经常看看的网站
    highcharts实例教程一:结合php与mysql生成折线图
    2015-2-10 ecshop
    一个简单的javascript获取URL参数的代码
    table 西边框样式
    PHP 获取当前日期及格式化
    mysql 获取当前日期及格式化
    mysql时间int日期转换
  • 原文地址:https://www.cnblogs.com/luckygxf/p/3592966.html
Copyright © 2011-2022 走看看