Subtracting two dates in python

MRAB python at mrabarnett.plus.com
Thu Jul 31 12:35:10 EDT 2014


On 2014-07-31 17:08, eshwar080 at gmail.com wrote:
> 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
>
Try the 'datetime' module:

 >>> from datetime import datetime
 >>> d = datetime.strptime("07/24/14", "%m/%d/%y")
 >>> d
datetime.datetime(2014, 7, 24, 0, 0)
 >>> diff = datetime.now() - d
 >>> diff.days
7



More information about the Python-list mailing list