针对检查日期范围日期在Python [英] Checking date against date range in Python

查看:227
本文介绍了针对检查日期范围日期在Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期变量: 2011-01-15 ,我想获得一个布尔回来,如果上述日期内是从今天起3天。我不是很清楚如何在Python构造此。我只是处理日期,而不是​​日期时间。

I have a date variable: 2011-01-15 and I would like to get a boolean back if said date is within 3 days from TODAY. Im not quite sure how to construct this in Python. Im only dealing with date, not datetime.

我的工作的例子是一个宽限期。用户登录到我的网站,如果在宽限期内是今天,其他脚本等3天为用户省略。

My working example is a "grace period". A user logs into my site and if the grace period is within 3 days of today, additional scripts, etc. are omitted for that user.

我知道你可以做一些花哨的/复杂的东西在Python的日期模块(S),但林不知道去哪里找。

I know you can do some fancy/complex things in Python's date module(s) but Im not sure where to look.

推荐答案

在Python来检查您可以使用 A℃的范围内; = X< = B

In Python to check a range you can use a <= x <= b:

>>> import datetime
>>> today = datetime.date.today()
>>> margin = datetime.timedelta(days = 3)

>>> today - margin <= datetime.date(2011, 1, 15) <= today + margin
True

这篇关于针对检查日期范围日期在Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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