python日历程序编写_怎样编写python 程序不使用time库的函数将指定日期转换为星期,要使用自定义函数进行换算?...

"""

日历的前提是

已知1900年1月1日是星期一 按照时间向后推 出现现在的日历

"""

# 判断年是否是闰年

def is_leap(year):

return year % 4 == 0 and year % 100 != 0 or year % 400 == 0

# 获取指定月的天数

def get_days(year, month):

if month in (1,3,5,7,8,10,12):

return 31

elif month in (4,6,9,11):

return 30

else:

# 因为判断是否是闰年的功能已经存在了 所以可以直接调用

return 29 if is_leap(year) else 28

# 获取指定年到1900经历的天数

def get_year_days(year):

year_days = 0 # 记录天数

for y in range(1900, year):

year_days += 366 if is_leap(y) else 365

return year_days

# 获取当前指定日期到 当前1月经历的天数

def get_month_days(year, month):

month_days = 0 # 记录天数

for m in range(1, month):

month_days += get_days(year, m)

return month_days

# 默认date接受的时间格式为"年-月-日"

def get_week(date):

# 按照横线切割

date_list = date.split("-")

print(date_list)

date_list = [int(ele) for ele in date_list]

print(date_list)

year, month, day = date_list # 取出年月日

# 获取年到1900年经历的天数

year_days = get_year_days(year)

# 获取指定月到当前年1月的天数

month_days = get_month_days(year, month)

# 统计到具体日期的天数

days = year_days + month_days + day

# 对7 整除 求星期几

week = days % 7

if week == 0:

return '星期日'

elif week == 1:

return '星期一'

elif week == 2:

return '星期二'

elif week == 3:

return '星期三'

elif week == 4:

return '星期四'

elif week == 5:

return '星期五'

else:

return '星期六'

week = get_week("2020-02-29")

print(week)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值