Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75422

Re: Subtracting two dates in python

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!gordon
From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: Subtracting two dates in python
Date Thu, 31 Jul 2014 16:31:11 +0000 (UTC)
Organization PANIX Public Access Internet and UNIX, NYC
Lines 38
Message-ID <lrdr0f$aaj$1@reader1.panix.com> (permalink)
References <ea9aa6cd-1fe4-420e-ac11-c3ff2be3befa@googlegroups.com>
NNTP-Posting-Host panix1.panix.com
X-Trace reader1.panix.com 1406824271 10579 166.84.1.1 (31 Jul 2014 16:31:11 GMT)
X-Complaints-To abuse@panix.com
NNTP-Posting-Date Thu, 31 Jul 2014 16:31:11 +0000 (UTC)
User-Agent nn/6.7.3
Xref csiph.com comp.lang.python:75422

Show key headers only | View raw


In <ea9aa6cd-1fe4-420e-ac11-c3ff2be3befa@googlegroups.com> eshwar080@gmail.com writes:

> 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

If you have the user-entered date in a string format, you can use
datetime.strptime() to convert it into a datetime object, like so:

    from datetime import datetime
    user_date = datetime.strptime(user_input, "%m/%d/%y")

Get the current system date like this:

    now_date = datetime.now()

Subtract the two datetime objects to obtain a timedelta object:

    mydelta = now_date - user_date

Look at the days attribute to get the difference in days:

   mydelta.days

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon@panix.com    watch 'House', or a real serial killer to watch 'Dexter'.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Subtracting two dates in python eshwar080@gmail.com - 2014-07-31 09:08 -0700
  Re: Subtracting two dates in python John Gordon <gordon@panix.com> - 2014-07-31 16:31 +0000
  Re: Subtracting two dates in python MRAB <python@mrabarnett.plus.com> - 2014-07-31 17:35 +0100

csiph-web