• 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]) 还可以计算剩余天数。

  • 相关阅读:
    Arm宣布将在Linux中应用事务内存扩展支持
    企业上云哪种形式的云更适合?
    AT&T推出云5G网络开源工具Airship
    GitHub源码攻击事件
    你应该选择哪种树莓派?
    Electron 5.0 发布
    Redox OS 发布 0.5 版
    经典的网络安全技术
    inux下:热插拔和模块是什么
    微软宣布全新命令行+脚本工具:PowerShell 7
  • 原文地址:https://www.cnblogs.com/Deng-Xian-Sheng/p/14452878.html
Copyright © 2020-2023  润新知