Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
758 views
in Technique[技术] by (71.8m points)

python - Find out if a date is more than 30 days old

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"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...