# -*- 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 =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 1 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 3 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 5 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 7 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 8 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 10 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 12: exceptSameMonthDay += 31 elif monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 4 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 6 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 9 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 11: exceptSameMonthDay += 30 elif monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 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 =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 1 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 3 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 5 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 7 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 8 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 10 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 12: exceptSameMonthDay += 31 elif monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 4 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 6 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 9 or monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 11: exceptSameMonthDay += 30 elif monthValue =https://www.cnblogs.com/Deng-Xian-Sheng/archive/2021/02/26/= 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]) 還可以計算剩余天數,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/264085.html
標籤:其他
上一篇:什么是JIT?怎么優化?
