Code to check for expiration date, from one python scripts output

10,005

You should be comparing datetime objects using strptime to create the datetime object from your expiration date string and comparing it to datetime.now().date(), strftime creates strings which will be compared lexicographically so you can get incorrect results:

from datetime import datetime, date
datet = '2015-12-15'

ExpirationDate = datetime.strptime(datet,"%Y-%m-%d").date()
now = date.today()
if ExpirationDate >= now:
    ....
Share:
10,005
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a pre made Python script that calls a C# script within an address server. The output of this script is:

    Build Number            : 2381
    Database Date           : 2015-07-15
    Database Expiration Date: 10-31-2015
    License Expiration Date : 2016-05-03
    
    
    Build Number            : 2381
    Database Date           : 2015-06-15
    Database Expiration Date: 2015-12-15
    License Expiration Date : 2016-05-03
    

    I want to be able to check today's date against the "License Expiration Date". I've looked over datetime and I am stumped. I know I can't check a date against an integer, but I just cant get it. This is what I have so far.

    import time
    print (time.strftime("%Y-%m-%d"))
    
    datet = '2015-12-15'
    
    class Timedelta(object):
        @property
        def isoformat(self):
               return str()
    
    ExpirationDate = time.strftime("%Y-%m-%d")
    if ExpirationDate >= datet:
        print 'Renew License Soon'
    elif ExpirationDate == datet:
        print 'Renew License Immediately'
    else:
        print "License OK"
        quit()
    
  • Admin
    Admin over 8 years
    Another issue is that once the license is renewed and granted an expiration date further in the future, whould it be best to just alter the script to reflect the change, or could the script be configured to automatically "update" to reflect the change? I appreciate all the help guys, but I'm not formally trained. Only been using Python for about a week and a half.
  • Padraic Cunningham
    Padraic Cunningham over 8 years
    Not sure I follow, what do you want the script to change?
  • Admin
    Admin over 8 years
    I don't really need the script to change anything. What I'm trying to accomplish is to somehow "grab" the expiration date from the output of the first python script and check it against today's date so that I can use that with monitoring software to let me know when my license is close to expiring. I'm sure I'm missing something essential, but I am at a loss.