SOLVED

DATEDIF problem

%3CLINGO-SUB%20id%3D%22lingo-sub-2325068%22%20slang%3D%22en-US%22%3EDATEDIF%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2325068%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20working%20at%20an%20application%20(internal%20use)%20requiring%20compatibility%20with%20the%20serial%20numbers%20generated%20by%20Excel%20for%20date%20values.%20During%20the%20testing%20phase%2C%20I've%20noticed%20strange%20issues%20(failing%20tests)%20which%20I've%20taken%20as%20my%20application's%20errors.%20Then%20I've%20created%20a%20table%20and%20I've%20noticed%20a%20strange%20behavior%20of%20the%20DATEDIF%20Excel%20function%20when%20using%20YD%20unit%20and%20MD%20unit%20(at%20least%2C%20this%20one%20is%20documented)%2C%20sometimes%20the%20manual%20calculation%20of%20the%20difference%20between%20the%20days%20of%20the%20years%20(the%20E%20column%20in%20the%20attached%20picture)%20gives%20a%20different%20result%20than%20the%20DATEDIF%20function.%20Where%20am%20I%20wrong%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2325068%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2325417%22%20slang%3D%22en-US%22%3ERe%3A%20DATEDIF%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2325417%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1045422%22%20target%3D%22_blank%22%3E%40vt1701%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet's%20take%20your%20first%20example%3A%2029%2F03%2F4976%20to%2015%2F10%2F5346%20(dd%2Fmm%2Fyyyy%20format).%3C%2FP%3E%0A%3CP%3EDATEDIF%20calculates%20the%20number%20of%20days%20as%20follows%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EMove%20the%20maximum%20number%20of%20whole%20years%20forward%20from%2029%2F03%2F4976%20to%20end%20up%20before%2015%2F10%2F5346.%20You%20arrive%20at%2029%2F03%2F5346.%3C%2FLI%3E%0A%3CLI%3ECount%20the%20days%20between%2029%2F03%2F5346%20to%2015%2F10%2F5346.%20This%20is%20200.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EWhy%20did%20you%20get%20a%20different%20result%3F%20Because%204976%20is%20a%20leap%20year.%20This%20causes%20the%20difference%20between%2001%2F01%2F4976%20and%2029%2F03%2F4976%20to%20be%201%20day%20more.%20But%20you%20don't%20want%20to%20count%20from%2001%2F01%2C%20but%20from%2029%2F03.%20(Since%205346%20is%20not%20a%20leap%20year%2C%20you%20don't%20count%20an%20extra%20day%20there)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20I%20would%20say%20that%20DATEDIF%20is%20correct%20here.%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hello,

 

I'm working at an application (internal use) requiring compatibility with the serial numbers generated by Excel for date values. During the testing phase, I've noticed strange issues (failing tests) which I've taken as my application's errors. Then I've created a table and I've noticed a strange behavior of the DATEDIF Excel function when using YD unit and MD unit (at least, this one is documented), sometimes the manual calculation of the difference between the days of the years (the E column in the attached picture) gives a different result than the DATEDIF function. Where am I wrong?

 

2 Replies
best response confirmed by allyreckerman (Microsoft)
Solution

@vt1701 

Let's take your first example: 29/03/4976 to 15/10/5346 (dd/mm/yyyy format).

DATEDIF calculates the number of days as follows:

  1. Move the maximum number of whole years forward from 29/03/4976 to end up before 15/10/5346. You arrive at 29/03/5346.
  2. Count the days between 29/03/5346 to 15/10/5346. This is 200.

Why did you get a different result? Because 4976 is a leap year. This causes the difference between 01/01/4976 and 29/03/4976 to be 1 day more. But you don't want to count from 01/01, but from 29/03. (Since 5346 is not a leap year, you don't count an extra day there)

 

So I would say that DATEDIF is correct here.

Thank you very much! Now I understand how the Excel function works, I was doing the other way in my algorithm.