1. 程式人生 > 其它 >時間判斷--自定義函式(1)

時間判斷--自定義函式(1)

時間判斷--XSY

import time
from datetime import datetime

def DateJudge(s_time):
    #輸入日期
    # s_time = 1381419600

    # 轉為時間陣列
    timeArray = time.strptime(s_time, "%m%d%Y")
    print(timeArray)

    otherStyleTime = time.strftime( "%m%d%Y", timeArray)
    print('目標時間',otherStyleTime)
    # 判斷月份
    if timeArray.tm_mon == 1:
        up_time = f'{timeArray.tm_year}-{timeArray.tm_mon}-01'
        week=datetime.strptime(up_time,  "%m%d%Y").weekday()
        print('1月星期', week)
        # 星期判斷
        if week > 4 :
            print("4*7= ",4*7)
            return 28
        elif week < 4 :
            print("5*7= ",5*7)
            return 35
    else:
        if timeArray.tm_mon in [2,4,5,7,8,10,11]:
            print(f'{timeArray.tm_mon}在2,4,5,7,8,10,11中,所以輸出:',28)
            return 28
        else:
            print(f'{timeArray.tm_mon}在3,6,9,12中,所以輸出:',35)
            return 35

DateJudge('03272022')