datetime模块

蓝桥杯练习系统
日期问题
题目描述
小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在 1960 年 1 月 1 日至 2059 年 12 月 31 日。令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。
更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。
比如 02/03/04,可能是 2002 年 03 月 04 日、2004 年 02 月 03 日或 2004 年 03 月 02 日。
给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?

a, b, c = map(str,input().split('/'))
aint = int(a)
bint = int(b)
cint = int(c)
arr = []
if aint >= 60 and aint <= 99 and bint >= 1 and bint <= 12 and cint >= 1 and cint <= 31:
    arr.append('19' + a + '-' + b + '-' + c)
elif aint >= 0 and aint <= 59 and bint >= 1 and bint <= 12 and cint >= 1 and cint <= 31:
    arr.append('20' + a + '-' + b + '-' + c)
if aint >= 1 and aint <= 12 and bint >= 1 and bint <= 31 and cint >= 60 and cint <= 99:
    arr.append('19' + c + '-' + a + '-' + b)
elif aint >= 1 and aint <= 12 and bint >= 1 and bint <= 31 and cint >= 0 and cint <= 59:
    arr.append('20' + c + '-' + a + '-' + b)
if aint >= 1 and aint <= 31 and bint >= 1 and bint <= 12 and cint >= 60 and cint <= 99:
    arr.append('19' + c + '-' + b + '-' + a)
elif aint >= 1 and aint <= 31 and bint >= 1 and bint <= 12 and cint >= 0 and cint <= 59:
    arr.append('20' + c + '-' + b + '-' + a)

arr = sorted(list(set(arr)))
num = len(arr)

from datetime import datetime

for i in range(num):
    try:
        answer = datetime.strptime(arr[i], "%Y-%m-%d")
        # 格式化日期对象为 '年-月-日' 格式的字符串
        print(answer.strftime("%Y-%m-%d"))
    except ValueError:
        continue
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值