描述
一个8位表示的年月日,如20200220,前4位表示年份,5-6位表示月份,7-8位表示日期。编写程序对日期数据进行处理和分析。
输入格式
第一行输入一个8位长度的日期形式(题目确保输入的字符串只包含0-9的数字,且长度为8位)
第二行输入一个分割符号sign
输出格式
- 根据输入数据中表示年份的数字判断是否为闰年。
- 输出当前月份共有多少天?
- 输出用sign符号分隔的日期输出,例如输入”/“,输出2020/09/09。
- 只考虑月份和日期的合法性,判断输入的数字是否为一个合法的日期。
- 输出当前月份的单词和缩写形式
输入输出示例 示例仅为格式展示,与测试数据无关
示例 1
输入:
20200229
/
输出:
2020年是闰年
2020年2月有29天
2020/02/29
20200229是合法日期
2月英文是February,缩写为Feb.
示例 2
输入:
18000231
@
输出:
1800年不是闰年
1800年2月有28天
1800@02@31
18000231是非法日期
2月英文是February,缩写为Feb.
# ------------ ------- -------- ----------- -----------
# @File : 6.4.3 日期分析处理实验模板.py
# @Contact : vasp@qq.com
# @Copyright : 2018-2025, Wuhan University of Technology
# @Modify Time: 2021/4/27 15:27
# @Author : 赵广辉
# @Version : 1.0
# @License : 仅限用于Python程序设计基础实践教程(赵广辉,高等教育出版社)配套实验
# ------------ ------- -------- ----------- -----------
def leap(current_date):
"""接收一个用8个字符表示日期的字符串为参数,判断这个日期中的年份是否为闰年
返回值为布尔型。
@参数 current_date:表示日期,字符串类型
闰年的判定方法是:能被400整除或能被4整除且同时不能被100整除的是闰年。
"""
year = int(current_date[:4])
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
return True
else:
return False
def legal_judge(current_date):
"""接收一个用8个字符表示日期的字符串为参数,判定日期的合法性,返回值为布尔型。
1,3,5,7,8,10,12月各31天,4,6,9,11各30天,闰年2月29天,平年2月28天。
@参数 current_date:表示日期,字符串类型
"""
month = int(current_date[4:6])
date = int(current_date[6:])
if month > 12:
return False
elif date > days_of_month(current_date):
return False
else:
return True
def days_of_month(current_date):
"""接收一个用8个字符表示日期的字符串为参数,若月份合法,返回这个月份有多少天的整数,
否则返回字符串"'**月份数字不合法'",**表示月份。
参数 current_date:表示日期,字符串类型
"""
if int(current_date[4:6]) <=12:
months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
current_month_order = int(current_date[4:6]) - 1
current_month_days = months[current_month_order]
if leap(current_date) and current_date[4:6] == '02':
current_month_days = current_month_days + 1
return current_month_days
else:
return f'{current_date[4:6]}月份数字不合法'
def name_of_month(current_date):
"""接收一个用8个字符表示日期的字符串为参数,返回这个月份对应的英文单词及其缩写形式。
9月缩写为'Sept.',此时返回值为:'September','Sept.'
"""
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December']
month_abbr = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'Jun.', 'Jul.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.']
month_order = int(current_date[4:6])-1
return months[month_order],month_abbr[month_order]
def separate_date(current_date, symbol):
"""接收一个用8个字符表示日期的字符串和一个符号为参数,返回用该符号分隔的日期,字符串类型。
@参数 current_date:表示日期,字符串类型
@参数 symbol:分隔符号,字符串类型
例如传入'20201031'和"/",返回字符串'2020/09/09'
"""
year = current_date[:4]
month = current_date[4:6]
date = current_date[6:]
return symbol.join([year, month, date])
def test(current_date):
if leap(current_date):
print(f'{current_date[:4]}年是闰年')
else:
print(f'{current_date[:4]}年不是闰年')
days = days_of_month(current_date)
print(f'{current_date[:4]}年{int(current_date[4:6])}月有{days}天')
print(separate_date(current_date, sign))
if legal_judge(current_date):
print(f'{current_date}是合法日期')
else:
print(f'{current_date}是非法日期')
month_name, month_abbr = name_of_month(current_date)
print(f'{int(current_date[4:6])}月英文是{month_name},缩写为{month_abbr}')
if __name__ == '__main__':
CurrentDate = input()
sign = input()
test(CurrentDate)