Date Subtraction

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rsutradhar_python

    #1

    Date Subtraction

    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.










  • Marc 'BlackJack' Rintsch

    #2
    Re: Date Subtraction

    In <mailman.7155.1 150525572.27775 .python-list@python.org >,
    rsutradhar_pyth on wrote:
    [color=blue]
    > 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.[/color]

    from datetime import date

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

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Cameron Laird

      #3
      Re: Date Subtraction

      In article <pan.2006.06.17 .06.42.06.80444 5@gmx.net>,
      Marc 'BlackJack' Rintsch <bj_666@gmx.net > wrote:[color=blue]
      >In <mailman.7155.1 150525572.27775 .python-list@python.org >,
      >rsutradhar_pyt hon wrote:
      >[color=green]
      >> 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.[/color]
      >
      >from datetime import date
      >
      >date_str_1 = '2006-01-10'
      >date_str_2 = '2005-12-15'
      >date_1 = date(*map(int, date_str_1.spli t('-')))
      >date_2 = date(*map(int, date_str_2.spli t('-')))
      >print (date_1 - date_2).days - 1[/color]

      Comment

      • Marc 'BlackJack' Rintsch

        #4
        Re: Date Subtraction

        In <c39gm3-29f.ln1@lairds. us>, Cameron Laird wrote:
        [color=blue]
        > In article <pan.2006.06.17 .06.42.06.80444 5@gmx.net>,
        > Marc 'BlackJack' Rintsch <bj_666@gmx.net > wrote:[color=green]
        >>In <mailman.7155.1 150525572.27775 .python-list@python.org >,
        >>rsutradhar_py thon wrote:[color=darkred]
        >>> 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.[/color]
        >>
        >>from datetime import date
        >>
        >>date_str_1 = '2006-01-10'
        >>date_str_2 = '2005-12-15'
        >>date_1 = date(*map(int, date_str_1.spli t('-')))
        >>date_2 = date(*map(int, date_str_2.spli t('-')))
        >>print (date_1 - date_2).days - 1[/color]
        > .
        > .
        > .
        > 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?[/color]

        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

        Comment

        Working...