confused on calculating date difference in days.

K

krishnakant Mane

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.
 
M

Marc 'BlackJack' Rintsch

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
 
B

Ben Finney

krishnakant Mane said:
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.
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
datetime.timedelta(-41, 55845)
 
B

Ben Finney

Marc 'BlackJack' Rintsch said:
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.
 
M

Marc 'BlackJack' Rintsch

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.


Back at you.

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

Ciao,
Marc 'BlackJack' Rintsch
 
K

krishnakant Mane

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.
 
D

Diez B. Roggisch

krishnakant said:
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
 
S

Shane Geiger

# 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
12000



krishnakant said:
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
(e-mail address removed) | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top