如何判断一个日期是否在Python的另外两个日期之间? [英] How to tell if a date is between two other dates in Python?

查看:1000
本文介绍了如何判断一个日期是否在Python的另外两个日期之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

日期,开始和结束都是格式为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. Thanks for any help!

推荐答案

由于您还不满意,我还有另一个答案。

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)

这篇关于如何判断一个日期是否在Python的另外两个日期之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆