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

摘要:
='False':#想判断是否含空格,效果不佳,已废弃;改用Python异常处理timeList=inputTime.split('-')#以“-”符分割字符串,赋值为列表#通过for循环+自定义的is_number函数判断列表是否含有字母trueList=[]fornumberValueintimeList:trueList.appendiftrueList.count==0:timeList=list#将列表从字符串转换为int整数iftimeList[0]%4==0ortimeList[0]%400==0:#判断是否为闰年。timeTotal=++29exceptSameMonthDay=0formonthValueinrange:#计算除当月在内的,已过天数;如:2009-11-14,不算第11月。仅算1~10月ifmonthValue==1ormonthValue==3ormonthValue==5ormonthValue==7ormonthValue==8ormonthValue==10ormonthValue==12:exceptSameMonthDay+=31elifmonthValue==4ormonthValue==6ormonthValue==9ormonthValue==11:exceptSameMonthDay+=30elifmonthValue==2:exceptSameMonthDay+=29returnexceptSameMonthDay+timeList[2]else:timeTotal=++28exceptSameMonthDay=0formonthValueinrange:ifmonthValue==1ormonthValue==3ormonthValue==5ormonthValue==7ormonthValue==8ormonthValue==10ormonthValue==12:exceptSameMonthDay+=31elifmonthValue==4ormonthValue==6ormonthValue==9ormonthValue==11:exceptSameMonthDay+=30elifmonthValue==2:exceptSameMonthDay+=28returnexceptSameMonthDay+timeList[2]else:return1#else:#return1else:return1timeValue=time.strftime#获取当前日期,并格式化#格式化成2020-02-26形式tipsValue='请输入日期,如:'+timeValue+':'inputTime=inputtry:inputTime=What_day_of_the_yearifinputTime==1:print('您的输入有误!
#-*- coding: UTF-8 -*-
importtime
defWhat_day_of_the_year(inputTime):

#函数,判断字符串是否为数字
    defis_number(s):
        try:
            float(s)
            returnTrue
        exceptValueError:
            pass

        try:
            importunicodedata
            unicodedata.numeric(s)
            returnTrue
        except(TypeError, ValueError):
            pass

        returnFalse

#判断字符串是否含有“-”分割符(是否按格式输入)
    if inputTime.find('-') != -1:
        #if '' in inputTime != 'False' or ' ' in inputTime != 'False': #想判断是否含空格(含空格不符合格式),效果不佳,已废弃;改用Python异常处理
            timeList = inputTime.split('-') #以“-”符分割字符串,赋值为列表
    
#通过for循环+自定义的is_number函数判断列表是否含有字母(含有字母不符合格式)
            trueList =[]
            for numberValue intimeList:
                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 1timeValue = 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] 改为returntimeTotal - (exceptSameMonthDay + timeList[2]) 还可以计算剩余天数。

免责声明:文章转载自《python输入年月日,得出已经过了多少天?》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇mac 无法识别seagate硬盘、无法向其写入文件angular6项目从零开始下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python自动化之pytest框架使用详解

pytest是一个非常成熟的全功能的Python测试框架,主要有以下几个特点: 简单灵活,容易上手 支持参数化 能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests) pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(...

python 时间模块小结(time and datetime)

一:经常使用的时间方法 1.得到当前时间 使用time模块,首先得到当前的时间戳 In [42]: time.time() Out[42]: 1408066927.208922 将时间戳转换为时间元组 struct_time In [43]: time.localtime(time.time()) Out[43]: time.struct_t...

Python打包工具

当我们完成一个完整的项目,需要该项目文件中打包成分发包共享给他人或者上传到pypi社区以供他人下载。这就需要对该项目进行打包分发。 项目文件 这是一个已写好的项目文件包,叫做my_pkg,它的目录结构如下: tutorial/ my_pkg/ __init__.py main.py utils/...

python基础知识4——collection类——计数器,有序字典,默认字典,可命名元组,双向队列

1、计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数。 ps:具备字典的所有功能 + 自己的功能  Counter 我们从中挑选一些相对常用的方法来举例: 在上面的例子我们可以看出,counter方法返回的是一个字典,它将字符串中出现的所有字符都进行了统计。在这里再介绍一下update方法,这个update方法是将两次...

Docker打包python flask服务

1、将宿主机上python环境保存到requirements.txt pip3 freeze >requirements.txt 2、新建sources.list文件(apt的源文件) sources.list具体内容如下: $ vi sources.list deb-src http://archive.ubuntu.com/ubuntu x...

第七章 Python 盒子:模块、包和程序

前戏部分 模块,用一堆(很多行)代码实现了某个功能的代码集合。 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来说,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块。 其实模块只是有很多 Python 代码的一个文...