confused on calculating date difference in days.

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Tue Oct 16 03:42:32 EDT 2007


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



More information about the Python-list mailing list