confused on calculating date difference in days.

Discussion in 'Python' started by krishnakant Mane, Oct 16, 2007.

  1. hello,
    I am strangely confused with a date calculation problem.
    the point is that I want to calculate difference in two dates in days.
    there are two aspects to this problem.
    firstly, I can't get a way to convert a string like "1/2/2005" in a
    genuan date object which is needed for calculation.
    now once this is done I will create a another date object with
    today = datetime.datetime.now()
    and then see the difference between this today and the string that I
    converted to date.
    now in the first place I can't recall how I can convert a string to a date.
    then now I don't know how to calculate difference in days between
    today and the string converted date.
    any help will be appreciated.
    regards,
    Krishnakant.
    krishnakant Mane, Oct 16, 2007
    #1
    1. Advertising

  2. On Tue, 16 Oct 2007 12:33:33 +0530, krishnakant Mane wrote:

    > firstly, I can't get a way to convert a string like "1/2/2005" in a
    > genuan date object which is needed for calculation.


    Why? Split the string up, convert the parts to `int` and just create a
    `datetime.date` object.

    > now once this is done I will create a another date object with
    > today = datetime.datetime.now()
    > and then see the difference between this today and the string that I
    > converted to date.
    > now in the first place I can't recall how I can convert a string to a date.
    > then now I don't know how to calculate difference in days between
    > today and the string converted date.


    In [421]: '1/2/2005'.split('/')
    Out[421]: ['1', '2', '2005']

    In [422]: map(int, '1/2/2005'.split('/'))
    Out[422]: [1, 2, 2005]

    In [423]: month, day, year = map(int, '1/2/2005'.split('/'))

    In [424]: a = datetime.date(year, month, day)

    In [425]: b = datetime.date.today() - a

    In [426]: b.days
    Out[426]: 1017

    Maybe you should read the docs next time. ;-)

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Oct 16, 2007
    #2
    1. Advertising

  3. krishnakant Mane

    Ben Finney Guest

    "krishnakant Mane" <> writes:

    > firstly, I can't get a way to convert a string like "1/2/2005" in a
    > genuan date object which is needed for calculation.


    Until recently, this was a wart in the Python standard library:
    datetime objects exist in the 'datetime' module, but parsing strings
    to get date/time components was only available in the 'time' module.

    In Python 2.5, though, we have 'datetime.datetime.strptime'
    <URL:http://docs.python.org/lib/datetime-datetime.html#l2h-634>, which
    parses a string according to a specified format, and returns the
    corresponding datetime value.

    >>> import datetime
    >>> datetime.datetime.strptime("2007-10-16t17:40:00", "%Y-%m-%dt%H:%M:%S")

    datetime.datetime(2007, 10, 16, 17, 40)

    > now once this is done I will create a another date object with
    > today = datetime.datetime.now()
    > and then see the difference between this today and the string that I
    > converted to date.


    That's simple: datetime objects support difference via the subtraction
    arithmetic operator, returning a datetime.timedelta instance
    <URL:http://docs.python.org/lib/datetime-timedelta.html>.

    >>> value = datetime.datetime(2007, 10, 16, 15, 30, 45)
    >>> value - datetime.datetime(2007, 10, 16, 15, 20, 00)

    datetime.timedelta(0, 645)
    >>> value - datetime.datetime(2007, 10, 12, 8, 25, 19)

    datetime.timedelta(4, 25526)
    >>> value - datetime.datetime(2007, 11, 26, 0, 0, 0)

    datetime.timedelta(-41, 55845)

    --
    \ "Experience is that marvelous thing that enables you to |
    `\ recognize a mistake when you make it again." -- Franklin P. |
    _o__) Jones |
    Ben Finney
    Ben Finney, Oct 16, 2007
    #3
  4. krishnakant Mane

    Ben Finney Guest

    Marc 'BlackJack' Rintsch <> writes:

    > On Tue, 16 Oct 2007 12:33:33 +0530, krishnakant Mane wrote:
    >
    > > firstly, I can't get a way to convert a string like "1/2/2005" in
    > > a genuan date object which is needed for calculation.

    >
    > Why? Split the string up, convert the parts to `int` and just
    > create a `datetime.date` object.


    What, re-implement 'strptime' in every program that needs it? And then
    debug the result every time?

    Even if one doesn't have Python 2.5 or above, surely getting the
    string parsed into int values by the standard 'time.strptime' is
    better than re-implementing it every time.

    > Maybe you should read the docs next time. ;-)


    Back at you.

    --
    \ "If you do not trust the source do not use this program." |
    `\ —Microsoft Vista security dialogue |
    _o__) |
    Ben Finney
    Ben Finney, Oct 16, 2007
    #4
  5. On Tue, 16 Oct 2007 18:10:54 +1000, Ben Finney wrote:

    > Marc 'BlackJack' Rintsch <> writes:
    >
    >> On Tue, 16 Oct 2007 12:33:33 +0530, krishnakant Mane wrote:
    >>
    >> > firstly, I can't get a way to convert a string like "1/2/2005" in
    >> > a genuan date object which is needed for calculation.

    >>
    >> Why? Split the string up, convert the parts to `int` and just
    >> create a `datetime.date` object.

    >
    > What, re-implement 'strptime' in every program that needs it? And then
    > debug the result every time?


    Yes. Seems easier to me. :)

    > Even if one doesn't have Python 2.5 or above, surely getting the
    > string parsed into int values by the standard 'time.strptime' is
    > better than re-implementing it every time.
    >
    >> Maybe you should read the docs next time. ;-)

    >
    > Back at you.


    Got me. I didn't know that `datetime` has a `strptime` now. I just
    looked at `date`.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Oct 16, 2007
    #5
  6. hello,
    thanks all of you for providing valuable help.
    right now I am confused about the delta object.
    how can I extract the difference between two dates in terms of day
    using the delta object?
    I tried reading the python docs but did not understand the concept of
    delta object and how can I measure the difference in terms of days
    between two dates.
    I expect that the days would be integers.
    secondly the format of my date is actually "16/10/2007", and this is
    all in varchar field inside a postgresql database.
    I understand that datetime.datetime.strptime would convert this string
    "16/10/2007" into a date object which I can then compare with the
    current date created by datetime.now().
    is that right?
    if yes then please explain me how I can get the delta object to give
    me results in days.
    regards,
    Krishnakant.
    krishnakant Mane, Oct 16, 2007
    #6
  7. krishnakant Mane wrote:

    > hello,
    > thanks all of you for providing valuable help.
    > right now I am confused about the delta object.
    > how can I extract the difference between two dates in terms of day
    > using the delta object?
    > I tried reading the python docs but did not understand the concept of
    > delta object and how can I measure the difference in terms of days
    > between two dates.
    > I expect that the days would be integers.
    > secondly the format of my date is actually "16/10/2007", and this is
    > all in varchar field inside a postgresql database.
    > I understand that datetime.datetime.strptime would convert this string
    > "16/10/2007" into a date object which I can then compare with the
    > current date created by datetime.now().
    > is that right?
    > if yes then please explain me how I can get the delta object to give
    > me results in days.


    The documentation is very clear about this:

    http://docs.python.org/lib/datetime-timedelta.html

    """
    Instance attributes (read-only):

    days Between -999999999 and 999999999 inclusive
    seconds Between 0 and 86399 inclusive
    microseconds Between 0 and 999999 inclusive
    """

    So (date_a - date_b).days will give you what you need.

    Diez
    Diez B. Roggisch, Oct 16, 2007
    #7
  8. krishnakant Mane

    Shane Geiger Guest

    # Example

    import datetime
    def days_old(birth_year=1974,birth_month=12,birth_day=7):
    return (datetime.date.today() -
    datetime.date(birth_year,birth_month,birth_day) ).days

    >>> days_old()

    12000



    krishnakant Mane wrote:
    > hello,
    > thanks all of you for providing valuable help.
    > right now I am confused about the delta object.
    > how can I extract the difference between two dates in terms of day
    > using the delta object?
    > I tried reading the python docs but did not understand the concept of
    > delta object and how can I measure the difference in terms of days
    > between two dates.
    > I expect that the days would be integers.
    > secondly the format of my date is actually "16/10/2007", and this is
    > all in varchar field inside a postgresql database.
    > I understand that datetime.datetime.strptime would convert this string
    > "16/10/2007" into a date object which I can then compare with the
    > current date created by datetime.now().
    > is that right?
    > if yes then please explain me how I can get the delta object to give
    > me results in days.
    > regards,
    > Krishnakant.
    >


    --
    Shane Geiger
    IT Director
    National Council on Economic Education
    | 402-438-8958 | http://www.ncee.net

    Leading the Campaign for Economic and Financial Literacy
    Shane Geiger, Oct 16, 2007
    #8
    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. David Stockwell
    Replies:
    2
    Views:
    687
    Anna Martelli Ravenscroft
    Sep 20, 2004
  2. Gerrit
    Replies:
    0
    Views:
    380
    Gerrit
    Sep 20, 2004
  3. Terry Reedy
    Replies:
    0
    Views:
    420
    Terry Reedy
    Sep 20, 2004
  4. clintonb
    Replies:
    9
    Views:
    854
    James Kanze
    May 23, 2007
  5. Paul E. Schoen

    Date difference in days

    Paul E. Schoen, Oct 10, 2010, in forum: Perl Misc
    Replies:
    11
    Views:
    197
    Paul E. Schoen
    Oct 13, 2010
Loading...

Share This Page