By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,490 Members | 1,417 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,490 IT Pros & Developers. It's quick & easy.

confused on calculating date difference in days.

P: n/a
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.
Oct 16 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Oct 16 '07 #2

P: n/a
"krishnakant Mane" <re**********@gmail.comwrites:
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
Oct 16 '07 #3

P: n/a
Marc 'BlackJack' Rintsch <bj****@gmx.netwrites:
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
Oct 16 '07 #4

P: n/a
On Tue, 16 Oct 2007 18:10:54 +1000, Ben Finney wrote:
Marc 'BlackJack' Rintsch <bj****@gmx.netwrites:
>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
Oct 16 '07 #5

P: n/a
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.
Oct 16 '07 #6

P: n/a
# 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
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
Oct 16 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.