Borrar filtros
Borrar filtros

A problem in calculating diffrence between two dates

2 visualizaciones (últimos 30 días)
googo
googo el 25 de Mzo. de 2013
function totalDays=dateDiff(date1,date2)
y1=date1(3);
y2=date2(3);
diff1 = dateDiffSameY(date1,[31 12 y1])+1;
diff2 = dateDiffSameY([1 1 y2],date2);
sum=0;
for i=(date1(3)+1):(date2(3)-1)
if checkLeapYear(i) == 0 % function to cheak if a year is leap
sum = sum + 364;
else
sum = sum + 365;
end
end
totalDays = diff1 + diff2 + sum;
end
Well, I don't see the problem. I know that there are some situations that I forgotten but for example if I enter dateDiff([22 2 1732],[14 12 1799]) my function return 24701 When using daysact I get 24767 .
Where is my mistake? Very new in matlab, hope for your help.
  2 comentarios
googo
googo el 25 de Mzo. de 2013
Editada: googo el 25 de Mzo. de 2013
ok.. I think the problem was fixed by adding +1 to each sum in the loop
Jan
Jan el 25 de Mzo. de 2013
Editada: Jan el 25 de Mzo. de 2013
Please use meaningful tags. "matlab" and "matlab code" do not help in a Matlab forum.
I cannot guess, what "daysact", "dateDiffSameY" and "checkLeapYear" is.
Do not shadow the builtin function "sum" by a variable of the same name. This leads to unexpected problems frequently.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 25 de Mzo. de 2013
The difference bertween dates can be calculated much easier:
function result = dateDiff(a, b)
result = datenum(a) - datenum(b);

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by