python之判斷合法日期

2022-05-02 20:18:05 字數 1180 閱讀 9007

年月日分別為自定義函式的引數,判斷某乙個日期是否為合法的日期;

如: 2023年12月33日不是合法的日期

2023年2月29日是不合法的日期

看**:方法一

def fn3(year, month, day):

if month > 12 or month <= 0:

return "%s年%s月%s日不是合法日期"%(year,month,day)

if num_6(year):

months = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

if day <= months[month-1]:

return "合法日期"

else:

return "%s年%s月%s日不是合法日期" % (year, month, day)

else:

month2 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

if day <= month2[month - 1]:

return "合法日期"

else:

return "%s年%s月%s日不是合法日期" % (year, month, day)

# print(fn3(2020, 1, 31))

#方法二

def youhua(year, month, day):

if month < 1 or month >12:

return false

days = 31

if month in [4,6,9,11]:

days = 30

elif month == 2:

if num_6(year):

days = 29

else:

days = 28

if day < 1 and day>days:

return false

return true

# print(youhua(2020, 1, 31))

兩種方法,前面的思路更加複雜

後面的複雜度會好很多,哎,邏輯思維還需要提高....................

寫**太難了,衝衝衝

Java判斷日期是否合法

記錄一下專案中遇到的乙個bug,當時原有工具類中的 如下 public static boolean isdate string str catch exception e return result 當輸入字串為2018 02 31時返回true,日期預設轉換為了2018 03 03,未對時間格式...

python合法日期 python日期處理

3.日期比較操作 在datetime模組中有timedelta類,用於表示乙個時間間隔。import datetime datetime.timedelta days 0,seconds 0,microseconds 0,milliseconds 0,minutes 0,hours 0,weeks ...

判斷日期是否合法的方法

public static boolean chkdateformat string date int year integer.parseint date.substring 0,4 int month integer.parseint date.substring 4,6 1 int day i...