[SOLVED]MonthsBetween() function behave differently with (03, 04, 05,07) lazarus / 62-General ---------------------------------------------------------------------- kito · 2021-08-05 21:58 Hello,  I made a little vacancy project I tested the function on random dates everything was perfect. unfortunately, I never thought  it will calculate differently  with those 04 months (March, April, May, July)  please notice that date formatting: YYYY/mm/dd MonthsBetween(2021/01/01,2021/12/31)  ------------>  11 MonthsBetween(2021/02/01,2021/12/31)  ------------>  10 MonthsBetween(2021/03/01,2021/12/31)  ------------>  10  expected to be 09 MonthsBetween(2021/04/01,2021/12/31)  ------------>  09  expected to be 08 MonthsBetween(2021/05/01,2021/12/31)  ------------>  08  expected to be 07 MonthsBetween(2021/06/01,2021/12/31)  ------------>  06 MonthsBetween(2021/07/01,2021/12/31)  ------------> 06  expected to be 05 MonthsBetween(2021/08/01,2021/12/31)  ------------>  04  MonthsBetween(2021/09/01,2021/12/31)  ------------>  03 MonthsBetween(2021/10/01,2021/12/31)  ------------>  02 MonthsBetween(2021/11/01,2021/12/31)  ------------>  01 MonthsBetween(2021/12/01,2021/12/31)  ------------>  00 I  already printed all the Holiday statements for the employees  I made a huge mess my boss is so angry    unlike some of my coworkers  So  what's wrong, is that normal and exactly how it suppose to work ? ---------------------------------------------------------------------- wp · 2021-08-05 22:23 Look at the source code (CTRL+click on the identifier "MonthsBetween" in your code to open its source file): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Function MonthsBetween(const ANow, AThen: TDateTime; AExact : Boolean = False): Integer; var  y, m, d: Word; begin  if AExact and (ANow >= -DateDelta) and (AThen >= -DateDelta) and     (ANow <= MaxDateTime) and (AThen <= MaxDateTime) then    begin    PeriodBetween(ANow, AThen, y, m, d);    Result := y*12 + m;    end  else    Result:=Trunc((Abs(DateTimeDiff(AN ow,AThen))+HalfMilliSecond)/ApproxDaysPerMonth);end; You see that there is a "hidden" argument "Exact" which is false by default. In this case, the function calculates the DateTimeDiff between the two dates in TDataeTime units (days), and in order to get months it divides that by the average days per month which you can find in the top of the unit to have the value 30.4375. But since the exact length of a month depends on the month itself (and the year in case of February) the result will not be "exact". This behaviour is documented in the help file of MonthsBetween: "MonthsBetween returns the number of whole months between ANow and AThen. This number is an approximation, based on an average number of days of 30.4375 per month (average over 4 years). This means the fractional part of a month is dropped." However, when you explicitely call the function with the "Exact" parameter set to true then the function calculates the difference between the two dates as years, months, and days. Now it neglects the days and takes the difference in month values (plus the 12-fold difference in year values). Now the results are as expected. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;uses  SysUtils, DateUtils;var  d1, d2: TDateTime;  m, mBetween: Integer;begin  d2 := EncodeDate(2021, 12, 31);  for m := 1 to 12 do  begin    d1 := EncodeDate(2021, m, 1);    mBetween := MonthsBetween(d2, d1, true);    WriteLn(DateToStr(d1), '...', DateToStr(d2), ' = ', mBetween, ' months');  end;  ReadLn;end. ---------------------------------------------------------------------- engkin · 2021-08-05 23:14 Lol,  How come DaysBetween function doesn't have this exact extra feature? ---------------------------------------------------------------------- kito · 2021-08-05 23:27 thank you for this generous clarification and detailed examples, indeed I must always check the source code, and the documentation. like it said  never build decisions by guessings or assuming thanks again I really appreciate all your efforts in this forum. ---------------------------------------------------------------------- lucamar · 2021-08-05 23:33 Quote from: engkin on August 06, 2021, 01:14:45 amHow come DaysBetween function doesn't have this exact extra feature? Because it calculates the exact value always: a simple substraction ... with a small milliseconds adjustment to cope with floating-point weirdness.