python输入年月日,得出已经过了多少天?

# -*- coding: UTF-8 -*-
import time
def What_day_of_the_year(inputTime):

#函数,判断字符串是否为数字
    def is_number(s):
        try:
            float(s)
            return True
        except ValueError:
            pass

        try:
            import unicodedata
            unicodedata.numeric(s)
            return True
        except (TypeError, ValueError):
            pass

        return False

#判断字符串是否含有“-”分割符(是否按格式输入)
    if inputTime.find('-') != -1:
        #if '' in inputTime != 'False' or ' ' in inputTime != 'False': #想判断是否含空格(含空格不符合格式),效果不佳,已废弃;改用Python异常处理
            timeList = inputTime.split('-') #以“-”符分割字符串,赋值为列表
    
#通过for循环+自定义的is_number函数判断列表是否含有字母(含有字母不符合格式)
            trueList = []
            for numberValue in timeList:
                trueList.append(is_number(numberValue))
            if trueList.count('False') == 0:

                timeList = list(map(int, timeList)) #将列表从字符串转换为int整数
                if timeList[0] % 4 == 0 or timeList[0] % 400 == 0: #判断是否为闰年。
                    timeTotal = (7 * 31) + (4 * 30) + 29

                    exceptSameMonthDay = 0
                    for monthValue in range(1,timeList[1]): #计算除当月在内的,已过天数;如:2009-11-14,不算第11月。仅算1~10月
                        if monthValue == 1 or monthValue == 3 or monthValue == 5 or monthValue == 7 or monthValue == 8 or monthValue == 10 or monthValue == 12:
                            exceptSameMonthDay += 31
                        elif monthValue == 4 or monthValue == 6 or monthValue == 9 or monthValue == 11:
                            exceptSameMonthDay += 30
                        elif monthValue == 2:
                            exceptSameMonthDay += 29
                    return exceptSameMonthDay + timeList[2]
                else:
                    timeTotal = (7 * 31) + (4 * 30) + 28

                    exceptSameMonthDay = 0
                    for monthValue in range(1,timeList[1]):
                        if monthValue == 1 or monthValue == 3 or monthValue == 5 or monthValue == 7 or monthValue == 8 or monthValue == 10 or monthValue == 12:
                            exceptSameMonthDay += 31
                        elif monthValue == 4 or monthValue == 6 or monthValue == 9 or monthValue == 11:
                            exceptSameMonthDay += 30
                        elif monthValue == 2:
                            exceptSameMonthDay += 28
                    return exceptSameMonthDay + timeList[2]
            else:
                return 1
        #else:
            #return 1
    else:
        return 1
timeValue = time.strftime("%Y-%m-%d", time.localtime()) #获取当前日期,并格式化
# 格式化成2020-02-26形式

tipsValue = '请输入日期,如:' + timeValue + ':'
inputTime = input(tipsValue)
try:
    inputTime = What_day_of_the_year(inputTime)
    if inputTime == 1:
        print('您的输入有误!')
    else:
        print(inputTime)
except :
    print('您的输入有误!')

如把 return exceptSameMonthDay + timeList[2] 改为 return timeTotal - (exceptSameMonthDay + timeList[2]) 还python基础教程可以计算剩余天数。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值