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


Groups > comp.lang.python > #75421 > unrolled thread

Subtracting two dates in python

Started byeshwar080@gmail.com
First post2014-07-31 09:08 -0700
Last post2014-07-31 17:35 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#75421 — Subtracting two dates in python

Fromeshwar080@gmail.com
Date2014-07-31 09:08 -0700
SubjectSubtracting two dates in python
Message-ID<ea9aa6cd-1fe4-420e-ac11-c3ff2be3befa@googlegroups.com>
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

[toc] | [next] | [standalone]


#75422

FromJohn Gordon <gordon@panix.com>
Date2014-07-31 16:31 +0000
Message-ID<lrdr0f$aaj$1@reader1.panix.com>
In reply to#75421
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'.

[toc] | [prev] | [next] | [standalone]


#75423

FromMRAB <python@mrabarnett.plus.com>
Date2014-07-31 17:35 +0100
Message-ID<mailman.12472.1406824701.18130.python-list@python.org>
In reply to#75421
On 2014-07-31 17:08, eshwar080@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web