Date Subtraction

Discussion in 'Python' started by rsutradhar_python, Jun 17, 2006.

  1. How to subtract date which is stored in string variable?

    Example:

    date1="2006-01-10"
    date2="2005-12-15"
    date = date1 - date2
    should give me 25 but problem is that date1 and date2 datatype is
    string which need to be conerted into date fromat which i am not able
    to do so please help me.
    rsutradhar_python, Jun 17, 2006
    #1
    1. Advertising

  2. In <>,
    rsutradhar_python wrote:

    > How to subtract date which is stored in string variable?
    >
    > Example:
    >
    > date1="2006-01-10"
    > date2="2005-12-15"
    > date = date1 - date2
    > should give me 25 but problem is that date1 and date2 datatype is
    > string which need to be conerted into date fromat which i am not able
    > to do so please help me.


    from datetime import date

    date_str_1 = '2006-01-10'
    date_str_2 = '2005-12-15'
    date_1 = date(*map(int, date_str_1.split('-')))
    date_2 = date(*map(int, date_str_2.split('-')))
    print (date_1 - date_2).days - 1

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Jun 17, 2006
    #2
    1. Advertising

  3. In article <>,
    Marc 'BlackJack' Rintsch <> wrote:
    >In <>,
    >rsutradhar_python wrote:
    >
    >> How to subtract date which is stored in string variable?
    >>
    >> Example:
    >>
    >> date1="2006-01-10"
    >> date2="2005-12-15"
    >> date = date1 - date2
    >> should give me 25 but problem is that date1 and date2 datatype is
    >> string which need to be conerted into date fromat which i am not able
    >> to do so please help me.

    >
    >from datetime import date
    >
    >date_str_1 = '2006-01-10'
    >date_str_2 = '2005-12-15'
    >date_1 = date(*map(int, date_str_1.split('-')))
    >date_2 = date(*map(int, date_str_2.split('-')))
    >print (date_1 - date_2).days - 1

    .
    .
    .
    Apparently you understand the original poster better than I.
    What's with the "- 1"? If I read you correctly, you'd calculate
    that there are zero days between, for example,
    2006-01-13
    and
    2006-01-12
    Do I have that right?
    Cameron Laird, Jun 18, 2006
    #3
  4. In <>, Cameron Laird wrote:

    > In article <>,
    > Marc 'BlackJack' Rintsch <> wrote:
    >>In <>,
    >>rsutradhar_python wrote:
    >>> date1="2006-01-10"
    >>> date2="2005-12-15"
    >>> date = date1 - date2
    >>> should give me 25 but problem is that date1 and date2 datatype is
    >>> string which need to be conerted into date fromat which i am not able
    >>> to do so please help me.

    >>
    >>from datetime import date
    >>
    >>date_str_1 = '2006-01-10'
    >>date_str_2 = '2005-12-15'
    >>date_1 = date(*map(int, date_str_1.split('-')))
    >>date_2 = date(*map(int, date_str_2.split('-')))
    >>print (date_1 - date_2).days - 1

    > .
    > .
    > .
    > Apparently you understand the original poster better than I.
    > What's with the "- 1"? If I read you correctly, you'd calculate
    > that there are zero days between, for example,
    > 2006-01-13
    > and
    > 2006-01-12
    > Do I have that right?


    No that's not what I would calculate. I would do without the ``- 1`` but
    the OP wanted 25 as result. Without the substraction it's 26. ;-)

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Jun 18, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    15,700
    Eric Smith
    Jan 18, 2006
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,229
    Michael Borgwardt
    May 30, 2004
  3. August1
    Replies:
    4
    Views:
    342
    August1
    Sep 21, 2004
  4. Andy
    Replies:
    34
    Views:
    1,353
    Kelsey Bjarnason
    Jan 1, 2004
  5. aidy

    date time subtraction

    aidy, Jul 1, 2009, in forum: Ruby
    Replies:
    3
    Views:
    112
Loading...

Share This Page