Date Subtraction

R

rsutradhar_python

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

Marc 'BlackJack' Rintsch

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
 
C

Cameron Laird

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
.
.
.
Apparently you understand the original poster better than I.
What's with the "- 1"? If I read you correctly, you'd calculate
that there are zero days between, for example,
2006-01-13
and
2006-01-12
Do I have that right?
 
M

Marc 'BlackJack' Rintsch

.
.
.
Apparently you understand the original poster better than I.
What's with the "- 1"? If I read you correctly, you'd calculate
that there are zero days between, for example,
2006-01-13
and
2006-01-12
Do I have that right?

No that's not what I would calculate. I would do without the ``- 1`` but
the OP wanted 25 as result. Without the substraction it's 26. ;-)

Ciao,
Marc 'BlackJack' Rintsch
 

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,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top