如何只打印工作日的日期(而不是周末),按日期打印几天范围内的日期?

时间:2018-08-14 19:32:56

标签: python date range

我需要在python中弄清楚如何(在pesudo中)

x = month-begin
y = month-end

for range in (x through y) 
  print(dates of weekdays - Monday trough Friday)

2 个答案:

答案 0 :(得分:1)

使用date.weekday()。按照您的伪代码示例:

for day in range (x through y)
    if day.weekday() in range(0,5):
        print(day)

答案 1 :(得分:1)

使用datetime模块:

import datetime
current=datetime.datetime(2018,8,14,14,00)  
end=datetime.datetime(2018,12,1,14,00) 
while current<end:
    current+=datetime.timedelta(days=1)
    if current.weekday()<5:
        print(current.strftime("%-m/%-d"))