找出日期是否超过30天 [英] Find out if a date is more than 30 days old

查看:61
本文介绍了找出日期是否超过30天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 inserttion_date 是否早于30天。这应该可以检测到当前时间的分钟和秒。 insertion_date 中的值将从API动态获取。

I want to know if insertion_date is older than 30 days. This should detect down to the minute and second of the current time. The value in insertion_date will be dynamically pulled from an API.

当前代码仅检测到当天,即

Current code only detects up to the day, i need the accuracy to up to the second.

import datetime
import dateutil.parser

insertion_date = dateutil.parser.parse('2017-08-30 14:25:30')
right_now_30_days_ago = datetime.datetime.today() - datetime.timedelta(days=30)

print right_now_30_days_ago #2017-08-31 12:18:40.040594
print insertion_date #2017-08-30 14:25:30

if insertion_date > right_now_30_days_ago:
    print "The insertion date is older than 30 days"

else:
    print "The insertion date is not older than 30 days"


推荐答案

您需要在类似的行中做一些事情:

you need to do something on similar lines:

from datetime import datetime, timedelta
time_between_insertion = datetime.now() - insertion_date

if  time_between_insertion.days>30:
    print "The insertion date is older than 30 days"

else:
    print "The insertion date is not older than 30 days"

这篇关于找出日期是否超过30天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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