Date Subtraction

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat Jun 17 02:42:06 EDT 2006


In <mailman.7155.1150525572.27775.python-list at python.org>,
rsutradhar_python wrote:

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

from datetime import date

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

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list