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


Groups > comp.lang.python > #75422

Re: Subtracting two dates in python

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: Subtracting two dates in python
Date 2014-07-31 16:31 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <lrdr0f$aaj$1@reader1.panix.com> (permalink)
References <ea9aa6cd-1fe4-420e-ac11-c3ff2be3befa@googlegroups.com>

Show all headers | 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