0

Please tell me how to get days between two dates using two variables. I have already tried using below code but it does not work

   datea = datetime.strptime(finspltsix, "%Y-%m-%d")
   dateb = datetime.strptime(finspltseven, "%Y-%m-%d")
   myresult9 = datea - dateb
   print myresult9.days
Ramesh Puruganti
  • 101
  • 2
  • 2
  • 6

1 Answers1

0

Here's an attempt to reproduce your reported problem:

from datetime import datetime

finspltsix='2014-05-11'
finspltseven='2014-09-11'

datea = datetime.strptime(finspltsix, "%Y-%m-%d")
dateb = datetime.strptime(finspltseven, "%Y-%m-%d")
myresult9 = datea - dateb
print myresult9.days

This prints -123, which is perfectly correct: datea is indeed 123 days before dateb (check a calendar if you don't think that's right!-).

So your incredibly-fuzzy assertion that "it does not work" just cannot be substantiated -- it works perfectly fine.

Please edit your Q to show your complete code (as simplified as possible!), what you expect to see, what you actually see -- IOW, please respect the stack overflow standards you explicitly agreed to as a precondition of posting here. Once you do respect this site's standards as you have agreed to do, we'll all be better placed to help!

For example, if, as per your comment, you omitted the quotes and just had e.g

finspltsix=2014-05-11

this would be computing finspltsix as the number 1998 via two subtractions, upon which of course the strptime call would raise an exception. We can't possibly tell if that's the case, as you callously, wantonly, and deliberately chose to hide exactly what problem you saw (exception traceback, strange result, whatever!), presumably in order to make it impossible for us to help you (if that was indeed your goal, you succeeded pretty well so far -- but, what a strange goal that would be!-).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Hello Alex, Thank you for your suggestions. The above code is working fine if we give, assign dates to variables. Please see my code below – Ramesh Puruganti Feb 16 '15 at 07:05
  • @RameshPuruganti, you didn't post any code "below" this comment of yours. You only posted extra code in a comment to the Q showing no quotes, as I mentioned, thus doing a subtraction. "if we give, assign dates to variables" is pretty much a precondition of using the variables, so when does it not "work fine", that mysterious "does not work" you **never** explained?! – Alex Martelli Feb 16 '15 at 15:11
  • @Alex..Please see the below code selsql="SELECT input1, input2 FROM stp_automation_output WHERE function_name ='%s'" %('DATEDIFF') try: cursor.execute(selsql) results = cursor.fetchall() for row in results: finspltsix=row[0] finspltseven=row[1] except: print "Error: unable to fetch data" dateformat = "%Y-%m-%d" datea = datetime.strptime(finspltsix, dateformat) dateb = datetime.strptime(finspltseven, dateformat) myresult9 = dateb - datea print myresult9.days – Ramesh Puruganti Feb 18 '15 at 08:27
  • @RameshPuruganti, a lot of code in a comment is unreadable -- I mean, just look at your own comment! Rather, edit your Q to show this code in a well-formatted way, the relevant subset of your DB, what you expect, what you observe. IOW, help us to help you -- the normal StackOverflow behavior! – Alex Martelli Feb 18 '15 at 15:07