如何判断一个日期是否在另外两个日期之间?

编程入门 行业动态 更新时间:2024-10-24 06:30:32
本文介绍了如何判断一个日期是否在另外两个日期之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码:

if date in (start, end): print('in between') else: print('No!')

date、start和end都是1/1格式的变量.我应该怎么做才能打印出正确的结果?我尝试将日期设置为 10/2,从 3/14 开始,到 11/7 结束,它打印的是否!",这意味着它运行不正确.我想必须将它们格式化为日期格式,然后进行比较.

date, start and end are all variables with the format of 1/1. What should I do to have it print out the right result? i tried date as 10/2, start as 3/14 and end as 11/7 and it's print 'No!', which means it's not running right. I guess have to format them to a date format and then compare them.

推荐答案

既然你还不满意,我再给你一个答案.不使用日期时间和年份.

As you are still not satisfied, I have another answer for you. Without using datetime and year.

它只是使用内置元组并比较它们:

It just uses built-in tuples and comparing them:

d1 = (3, 28) d2 = (3, 31) d3 = (4, 2) if d1 < d2 < d3: print("BETWEEN!") else: print("NOT!")

您可以轻松地创建这样的元组:

You can create tuple like these easily:

day = 16 month = 4 d = (month, day)

更多推荐

如何判断一个日期是否在另外两个日期之间?

本文发布于:2023-10-23 22:42:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522214.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   如何判断   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!