debugging - Ruby : How to correctly calculate date difference? -


i began (date2 - date1).round, should have work.

problem :

06 jan,2013 05 feb,2013 => 30

06 feb,2013 05 mar,2013 => 27

06 mar,2013 05 apr,2013 => 30

06 apr,2013 27 apr,2013 => 21

(date.strptime('05 feb,2013', '%d %b, %y') - date.strptime('06 jan,2013', '%d %b,%y')).round (date.strptime('05 mar,2013', '%d %b, %y') - date.strptime('06 feb,2013', '%d %b,%y')).round (date.strptime('05 apr,2013', '%d %b, %y') - date.strptime('06 mar,2013', '%d %b,%y')).round (date.strptime('27 apr,2013', '%d %b, %y') - date.strptime('06 apr,2013', '%d %b,%y')).round 

therefore,

    total = 108 days [ 30 + 27 + 30 + 21 ] 

but when try calculate in 1 go :

(date.strptime('27 apr,2013', '%d %b, %y') - date.strptime('06 jan,2013', '%d %b,%y')).round 

this gives :

days = 111 days 

now, 108 days != 111 days

what doing wrong ?

you're missing 1 day between intervals in first code.

i increased end date of first 3 dates, matches start of next.

p (date.strptime('06 feb,2013', '%d %b, %y') - date.strptime('06 jan,2013', '%d %b,%y')).round p (date.strptime('06 mar,2013', '%d %b, %y') - date.strptime('06 feb,2013', '%d %b,%y')).round p (date.strptime('06 apr,2013', '%d %b, %y') - date.strptime('06 mar,2013', '%d %b,%y')).round p (date.strptime('27 apr,2013', '%d %b, %y') - date.strptime('06 apr,2013', '%d %b,%y')).round 

output:

31 28 31 21 

sum = 111


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -