3

I would like to subtract two dates

i.e I have entered a date into a textbox which is of type String like below

type(waitForObject(":VWAP Calculator_LCDateTextField"), "07/24/14")

I am capturing that date like below

Current = (waitForObject(":VWAP Calculator_LCDateTextField").text)

so, now I want to subtract the captured date with my current system date and get the difference in days. I tried many ways and see no success. Someone please help with this as soon as possible.

P.S: I have python 2.4 and 2.7

TheSoundDefense
  • 6,753
  • 1
  • 30
  • 42
Brother85
  • 73
  • 3
  • 10
  • Can you use either 2.4 or 2.7, or do you have a preference? I imagine the answers could be different depending on which one it is. – TheSoundDefense Jul 31 '14 at 16:07
  • 2
    You should look at the `datetime` module, specifically the section about `timedeltas`. What you're after is _built in_ functionality in Python. – g.d.d.c Jul 31 '14 at 16:07
  • @TheSoundDefense it's Python 2.7 – Brother85 Jul 31 '14 at 16:10
  • @g.d.d.c I have gone through that module and tried many ways but I always run into issues. Please suggest me – Brother85 Jul 31 '14 at 16:11
  • 3
    @eswar - If you have already tried some things, please include that information in your question. Describe what you've tried and how it failed. See http://stackoverflow.com/help/how-to-ask – Robᵩ Jul 31 '14 at 16:13
  • @TheSoundDefense: I get this error"Detail TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'time.struct_time' " when I followed the Alex solution – Brother85 Jul 31 '14 at 17:47
  • @eswar it sounds like you're getting a `time` object for the current time instead of `datetime`. They both need to be `datetime` for subtraction to work. – TheSoundDefense Jul 31 '14 at 18:39

2 Answers2

5

Try something like this:

>>> from datetime import datetime
>>> x = datetime.strptime("07/24/14", "%m/%d/%y")
>>> y = datetime.strptime("07/30/14", "%m/%d/%y")
>>> x-y
datetime.timedelta(-6)
>>> (x-y).days
-6

This uses the datetime.strptime method of converting a string to a datetime object. Then you can simply do arithmetic on datetime objects. The result will be in a timedelta (basically a difference of time) which you can then call .days on if you want the difference to be in days. You can read about all these functions here.

If you want to get current date using these methods, it's as simple as:

>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2014, 7, 31, 12, 16, 12, 966428)

And it returns another datetime object which you can then perform the date arithmetic on.

  • I want to subtract the entered date in textbox with the system date Alex. – Brother85 Jul 31 '14 at 16:14
  • @eswar there's no way you can do that without converting your string to something else. Alex's answer gives what is probably the easiest method of doing so. – TheSoundDefense Jul 31 '14 at 16:15
  • @TheSoundDefense:I have converted the string into datetype below Current = datetime.Parse(waitForObject(":VWAP Calculator_LCDateTextField").text) dt1 = (waitForObject(":VWAP Calculator_LCDateTextField").text) entereddate =datetime.strftime(dt1, "%Y/%m/%d") how do I proceed from here – Brother85 Jul 31 '14 at 16:19
  • @eswar get the current `datetime`, as Alex showed above, and then subtract and get the number of days, which he also showed above. – TheSoundDefense Jul 31 '14 at 16:21
  • Is this below code correct. Current = datetime.Parse(waitForObject(":VWAPCalculator_LCDateTextField").text) dt1 = (waitForObject(":VWAP Calculator_LCDateTextField").text) entereddate =datetime.strftime(dt1, "%Y/%m/%d") dt2= datetime.now() dt2.dt1 – Brother85 Jul 31 '14 at 16:27
  • AFter getting the days. How do we convert it into a string value./ – Brother85 Jul 31 '14 at 17:09
  • str(days) will turn the integer into a string –  Jul 31 '14 at 18:18
0
  1. Parse the string to datetime objects (Converting string into datetime)

  2. Subtract the datetime objects, you'll get a timedelta object which has days property

delta = dtend - dtstart
print delta.days
Community
  • 1
  • 1
Imran
  • 87,203
  • 23
  • 98
  • 131
  • I have tried this Current = datetime.Parse(waitForObject(":VWAP Calculator_LCDateTextField").text) dt1 = (waitForObject(":VWAP Calculator_LCDateTextField").text) entereddate =datetime.strftime(dt1, "%Y/%m/%d") – Brother85 Jul 31 '14 at 16:15
  • How do I proceed from here – Brother85 Jul 31 '14 at 16:20