#-*- coding:utf-8 -#-
#print calendar
#Author Name:Ailsa
year=int(input('Please input year:'))
if year<1970 &year>3000:
print("输入年份范围为:1970-3000!!!")
month=int(input('Please input month:'))
if month<1 & month >12:
print("输入月份范围为:1-12!!!")
#2月份的输入判断放在后面再加
date=int(input('Please input date:'))
if date<1 & date>31:
print("输入日期范围为:1-31!!!")
#计算是否是闰年,大于3200年的还未加判断
"""if year % 4==0:
if year % 100==0:
if year % 400==0:
print(year,'是闰年')
else: print(year,'不是闰年')
else: print(year,'是闰年')
else: print(year,'不是闰年')
"""
def year_r(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return 1
else:
return 0
else:
return 1
else:
return 0
#计算year-month-date中间隔了多少天
Count_r=0
for i in range(1970,year):
if year_r(i)==1:
Count_r=Count_r + year_r(i)
#print('共有',Count_r,'闰年')
#截止到year这一年1月1日有多少天
yeardate=(year-1970-Count_r)*365+Count_r*366
#计算从月开始的日期
def monthdatefun(month,date):
if month==1:
monthdate=date
return monthdate
elif month==2:
monthdate=31+date
return monthdate
elif month==3:
if year_r(year)==1:
monthdate=31+29+date
return monthdate
else: monthdate=31+28+date
return monthdate
elif month==4:
if year_r(year)==1:
monthdate=31+29+31+date
return monthdate
else: monthdate=31+28+31+date
return monthdate
elif month==5:
if year_r(year)==1:
monthdate=31+29+31+30+date
return monthdate
else: monthdate=31+28+31+30+date
return monthdate
elif month==6:
if year_r(year)==1:
monthdate=31+29+31+30+31+date
return monthdate
else: monthdate=31+28+31+30+31+date
return monthdate
elif month==7:
if year_r(year)==1:
monthdate=31+29+31+30+31+30+date
return monthdate
else: monthdate=31+28+31+30+31+30+date
return monthdate
elif month==8:
if year_r(year)==1:
monthdate=31+29+31+30+31+30+31+date
return monthdate
else: monthdate=31+28+31+30+31+30+31+date
return monthdate
elif month==9:
if year_r(year)==1:
monthdate=31+29+31+30+31+30+31+31+date
return monthdate
else: monthdate=31+28+31+30+31+30+31+31+date
return monthdate
elif month==10:
if year_r(year)==1:
monthdate=31+29+31+30+31+30+31+31+30+date
return monthdate
else: monthdate=31+28+31+30+31+30+31+31+30+date
return monthdate
elif month==11:
if year_r(year)==1:
monthdate=31+29+31+30+31+30+31+31+30+31+date
return monthdate
else: monthdate=31+28+31+30+31+30+31+31+30+31+date
return monthdate
elif month==12:
if year_r(year)==1:
monthdate=31+29+31+30+31+30+31+31+30+31+30+date
return monthdate
else: monthdate=31+28+31+30+31+30+31+31+30+31+30+date
return monthdate
#Timedate=monthdatefun(month,date)+yeardate
#print('距离1970年一共:',Timedate)
#计算当天是周几
def weekend(Timedate,date):
if Timedate%7==0:
# print(year,'-',month,'-',date,'是星期三')
print(' '*13,'%-7d'%date,end='')
return 3
elif Timedate%7==1:
# print(year,'-',month,'-',date,'是星期四')
print(' '*20,'%-7d'%date,end= '')
return 4
elif Timedate%7==2:
# print(year,'-',month,'-',date,'是星期五')
print(' '*27,'%-7d'%date,end='')
return 5
elif Timedate%7==3:
# print(year,'-',month,'-',date,'是星期六')
print(' '*34,'%-7d'%date,end='')
return 6
elif Timedate%7==4:
# print(year,'-',month,'-',date,'是星期日')
print(' '*41,'%-7d'%date,end='')
return 7
elif Timedate%7==5:
# print(year,'-',month,'-',date,'是星期一')
print('%-07d'%date,end='')
return 1
elif Timedate%7==6:
# print(year,'-',month,'-',date,'是星期二')
print(' '*6,'%-7d'%date,end='')
return 2
#打印日期
print('星期一','星期二','星期三','星期四','星期五','星期六','星期日')
def monthbig(month):
Timedate = monthdatefun(month, 1) + yeardate
flag = weekend(Timedate,1)
for i in range(2, 32):
if flag % 7 == 0:
print('')
flag = flag + 1
# if i == date:
# print('<', date, '>', end=' ')
# else:
print('%-06d ' %i, end='')
def monthsmall(month):
Timedate=monthdatefun(month,1)+yeardate
flag=weekend(Timedate,1)
for i in range(2,31):
if flag % 7 == 0:
print('')
flag = flag + 1
# if i==date:
# print('<',date,'>',end=' ')
# else:
print('%-06d ' %i, end='')
def monthFeb(month):
Timedate = monthdatefun(month, 1) + yeardate
flag = weekend(Timedate,1)
if year_r(year):
for i in range(2, 30):
if flag % 7 == 0:
print('')
flag = flag + 1
# if i == date:
# print('<', date, '>', end=' ')
# else:
print('%-06d '%i, end='')
else:
Timedate = monthdatefun(month, 1) + yeardate
flag = weekend(Timedate,1)
for i in range(2, 29):
if flag % 7 == 0:
print('')
flag = flag + 1
# if i == date:
# print('<', date, '>', end=' ')
# else:
print('%-06d ' % i, end='')
if month==1:
monthbig(1)
elif month==2:
monthFeb(2)
elif month==3:
monthbig(3)
elif month==4:
monthsmall(4)
elif month==5:
monthbig(5)
elif month==6:
monthsmall(6)
elif month==7:
monthbig(7)
elif month==8:
monthbig(8)
elif month==9:
monthsmall(9)
elif month==10:
monthbig(10)
elif month==11:
monthsmall(11)
else:monthbig(12)
有些注释的地方没有删掉,因为后期学深入了还想继续优化。勿喷。
以上写了这么多,其实有个更简单的方法,只需要两句话就可以搞定,可见站在巨人的肩膀上,果然事半功倍
import claendar
print(calendar.month(2018,8)) #打印2018年8月的日历