By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

sysdate - 5 days

P: 1
Hi,

I new to python.

I'm trying to make changes to an existing python code .
The code has an if condition something like

if datetime(*(time.strptime(creation_date, '%d-%b-%y')[0:6])) < datetime(2006,1,1):


now instead of using datetime(2006,1,1) on the right hand side of the if condition, I wish to have a variable that is (sysdate-5days )??

thanks for any help
Jan 5 '07 #1
Share this Question
Share on Google+
4 Replies


bartonc
Expert 5K+
P: 6,596
Hi,

I new to python.

I'm trying to make changes to an existing python code .
The code has an if condition something like

if datetime(*(time.strptime(creation_date, '%d-%b-%y')[0:6])) < datetime(2006,1,1):


now instead of using datetime(2006,1,1) on the right hand side of the if condition, I wish to have a variable that is (sysdate-5days )??

thanks for any help
Hello and welcome to the python forum. Here are some things to get you started:

>>> import datetime
>>> offsetTime = datetime.timedelta(-5) # negative 5 days

>>> varDate = datetime.date(datetime.MINYEAR,1,1)
>>> varDate = varDate.today()
>>> varDate
datetime.date(2007, 1, 5)
>>> varDate + offsetTime
datetime.date(2006, 12, 31)
>>>
Jan 5 '07 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Maybe this will help:
Expand|Select|Wrap|Line Numbers
  1. >>> datetime.datetime(1,1,1).today() - datetime.timedelta(3)
  2. datetime.datetime(2007, 1, 2, 8, 49, 30, 468000)
  3. >>> print datetime.datetime(2007, 1, 2, 8, 49, 30, 468000)
  4. 2007-01-02 08:49:30.468000
  5. >>> datetime.datetime(1,1,1).today() - datetime.timedelta(weeks=3)
  6. datetime.datetime(2006, 12, 15, 8, 51, 3, 546000)
  7. >>> print datetime.datetime(2006, 12, 15, 8, 51, 3, 546000)
  8. 2006-12-15 08:51:03.546000
  9. >>> 
Jan 5 '07 #3

bvdet
Expert Mod 2.5K+
P: 2,851
More information that may be useful:
Expand|Select|Wrap|Line Numbers
  1. print '\nDifference between 2 datetime instances:'
  2. creation_date = datetime.datetime(2006,12,31,0,12,44)
  3. todays_date = datetime.datetime(1900,1,1).today()
  4. target_date = datetime.datetime(1900,1,1).today() - datetime.timedelta(5)
  5.  
  6. print 'creation_date =', creation_date
  7. print 'todays_date =', todays_date
  8. print 'target_date =', target_date
  9.  
  10. print
  11. print todays_date - creation_date
  12. print todays_date - target_date
  13.  
  14. if creation_date > target_date:
  15.     print "Greater"
  16. elif todays_date < target_date:
  17.     print "NOT Greater"
  18. else:
  19.     print "The dates are equal"
Output:
Expand|Select|Wrap|Line Numbers
  1. Difference between 2 datetime instances:
  2. creation_date = 2006-12-31 00:12:44
  3. todays_date = 2007-01-05 10:20:16.281000
  4. target_date = 2006-12-31 10:20:16.281000
  5.  
  6. 5 days, 10:07:32.281000
  7. 5 days, 0:00:00
  8. The dates are equal
Jan 5 '07 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. if creation_date > target_date:
  2.     print "Greater"
  3. elif todays_date < target_date:
  4.     print "NOT Greater"
  5. else:
  6.     print "The dates are equal"
Oops! Should be:
Expand|Select|Wrap|Line Numbers
  1. if creation_date > target_date:
  2.     print "Greater"
  3. elif creation_date < target_date:
  4.     print "NOT Greater"
  5. else:
  6.     print "The dates are equal"
Jan 5 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.