python時間模組

2022-05-10 04:17:47 字數 3532 閱讀 2703

import datetime

defget_current_day()

:'''

當日:return:

'''end_time = datetime.date.today(

) year = end_time.strftime(

"%y"

) month = end_time.strftime(

"%m"

) day = end_time.strftime(

"%d"

) params =

--"}

return params

defget_current_week()

:'''

當周:return:

'''start_time, end_time = datetime.date.today(

), datetime.date.today(

) one_day = datetime.timedelta(days=1)

while start_time.weekday()!=

0:start_time -= one_day

while end_time.weekday()!=

6:end_time += one_day

start_year = start_time.strftime(

"%y"

) start_month = start_time.strftime(

"%m"

) start_day = start_time.strftime(

"%d"

) year = end_time.strftime(

"%y"

) month = end_time.strftime(

"%m"

) end_day = end_time.strftime(

"%d"

) params =

--",

# 開始日期

"end_year"

: year,

# 結束年份

"end_month"

: month,

# 結束月份

"end_day"

: end_day,

# 結束日期

"end_date"

: f"--"

# 結束日期

}return params

defget_current_month()

:'''

當月當前時間的月份首日與最後一天

:return:

'''today = datetime.date.today(

) year, month = today.year, today.month

end = datetime.datetime(year, month +1,

1)- datetime.timedelta(days=1)

params =

--01"

,"end_day"

: f""

,"end_date"

: f"--",}

return params

defget_last_day()

:'''

昨天:return:

'''d = datetime.date.today(

) one_day = datetime.timedelta(days=1)

end_time = d - one_day

year = end_time.strftime(

"%y"

) month = end_time.strftime(

"%m"

) day = end_time.strftime(

"%d"

) params =

--"}

return params

defget_last_week()

:'''

上週:return:

'''d = datetime.date.today(

) days_count = datetime.timedelta(days=d.isoweekday())

end_time = d - days_count

six_days = datetime.timedelta(days=6)

start_time = end_time - six_days

start_year = start_time.strftime(

"%y"

) start_month = start_time.strftime(

"%m"

) start_day = start_time.strftime(

"%d"

) year = end_time.strftime(

"%y"

) month = end_time.strftime(

"%m"

) end_day = end_time.strftime(

"%d"

) params =

--",

"year"

: year,

"month"

: month,

"end_day"

: end_day,

"end_date"

: f"--",}

return params

# 上月

defget_last_month()

: d = datetime.date.today(

) days_count = datetime.timedelta(days=d.day)

end_time = d - days_count

year = end_time.strftime(

"%y"

) month = end_time.strftime(

"%m"

) params =

--01"

,"end_day"

: f""

,"end_date"

: f"--",}

return params

if __name__ ==

'__main__'

:print

('當日'

, get_current_day())

print

('當周'

, get_current_week())

print

('當月'

, get_current_month())

print

('昨日'

, get_last_day())

print

('上週'

, get_last_week())

print

('上月'

, get_last_month(

))

python 時間模組

import os import time s 2019 7 14 print time.strptime s,y m d s time 09 00 00 print time.strptime s time,h m s 把元組的時間轉換為時間戳 tuple time time.localtime ...

python 時間模組

格式化時間字串 y 兩位數的年份表示 00 99 y 四位數的年份表示 0000 9999 m 月份 01 12 d 月內的一天 0 31 h 24小時制的小時數 0 23 i 12小時制的小時數 01 12 m 分鐘數 00 59 s 秒 00 59 a 本地簡化星期名稱 a 本地完整星期名稱 b...

python 時間模組

時間的一般表示 import time s 2019 7 14 print time.strptime s,y m d 1.時間戳 print time.time 2.字串時間 print time.ctime 3.元組型別的時間 print time.localtime info time.loc...