You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Python工作日脚本的日期输出格式从YYYY/MM/DD改为DD/MM/YYYY?

修改日期输出格式为DD/MM/YYYY的方法

你只需要修改输出日期的那一行代码,利用date对象的strftime()方法自定义格式即可,具体操作:

  • 找到原代码中打印工作日的行:
    print("- " + str(day))
    
  • 将其替换为:
    print("- " + day.strftime("%d/%m/%Y"))
    

strftime()方法的格式说明符含义:

  • %d:两位数字表示的日(01-31)
  • %m:两位数字表示的月(01-12)
  • %Y:四位数字表示的年

修改后的完整代码如下:

from datetime import date, timedelta
import numpy as np
from calendar import monthrange

month = input("Month: ")
year = input("Year: ")

daysInMonth = monthrange(int(year), int(month))[1] # Extract the number of days in the month


sdate = date(int(year), int(month), 1)   # start date
edate = date(int(year), int(month), int(daysInMonth))   # end date

delta = edate - sdate       # as timedelta

numberOfDays = 0

for i in range(delta.days + 1):  # Look through all days in the month
    day = sdate + timedelta(days=i)
    if np.is_busday([day]): # Determine if it's a business day
        print("- " + day.strftime("%d/%m/%Y"))
        numberOfDays += 1

print("Working days:" + str(numberOfDays))

内容的提问来源于stack exchange,提问作者Steven Diffey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 10:45:32