{
  "schemaVersion": 1,
  "slug": "date-difference-calculator",
  "category": "date-time",
  "kind": "implementation",
  "independentValidation": false,
  "implementationHash": "3d82b00d9370196579914b2d139ff1e22a66d61c00fce73c0a1782ecab169b3a",
  "provenance": "Generated from the calculation contract used by the published page. Publication is not external validation.",
  "sourceFiles": [
    "data/calculators/date-difference-calculator.json",
    "src/engine.js"
  ],
  "runtimeSource": "/sources/engine.txt",
  "runtimeHash": "5c888d8898afbdee36267bb9977829e1e2ac568f6677c8ee87469c139864645c",
  "scope": "The compute function records implemented equations, constants, lookup tables and custom rules. Inputs record defaults and allowed values; outputs record precision and units. Defaults are examples, not recommendations. General background references are not evidence for this specific algorithm.",
  "publishedFormula": {
    "formula": "elapsed = |E − S|/86400000; counted = elapsed + (both endpoints ? 1 : 0); hours = 24 × counted; calendar months use clamped month shifts",
    "vars": [],
    "note": "Fractional years use 365.2425 days. The older time-between entry keeps its calendar years/months/days fields, while totalDays is the counted span."
  },
  "externalEvidence": null,
  "contract": {
    "compute": "try{if(false)throw new Error('inputError');const calculate=function(){const h=(function context() {\n  const DAY=86400000;\n  const own=(o,k)=>Object.prototype.hasOwnProperty.call(o,k);\n  const fail=()=>{throw new Error('inputError');};\n  const num=(v,min=-Infinity,max=Infinity,integer=false)=>{\n    if(!Number.isFinite(v)||v<min||v>max||(integer&&!Number.isInteger(v)))fail();return v;\n  };\n  const choice=(v,values)=>{if(!values.includes(v))fail();return v;};\n  const date=s=>{if(typeof s!=='string'||!/^\\d{4}-\\d{2}-\\d{2}$/.test(s)||s.startsWith('0000'))fail();const d=new Date(s+'T00:00:00Z');if(!Number.isFinite(+d)||d.toISOString().slice(0,10)!==s)fail();return d;};\n  const iso=d=>{if(!Number.isFinite(+d)||d.getUTCFullYear()<1||d.getUTCFullYear()>9999)fail();return d.toISOString().slice(0,10);};\n  const shift=(d,n)=>{const r=new Date(+d+n*DAY);iso(r);return r;};\n  const month=(d,n)=>{const r=new Date(+d);r.setUTCDate(1);r.setUTCMonth(r.getUTCMonth()+n);const end=new Date(+r);end.setUTCMonth(end.getUTCMonth()+1,0);r.setUTCDate(Math.min(d.getUTCDate(),end.getUTCDate()));iso(r);return r;};\n  const weekdays=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];\n  const work=(d,count)=>{let n=Math.floor(count/7)*5;for(let k=0;k<count%7;k++){const w=(d.getUTCDay()+k)%7;if(w!==0&&w!==6)n++;}return n;};\n  const wholeMonths=(a,b)=>{let n=(b.getUTCFullYear()-a.getUTCFullYear())*12+b.getUTCMonth()-a.getUTCMonth();if(n>=0&&+month(a,n)>+b)n--;if(n<0&&+month(a,n)<+b)n++;return n;};\n  return {DAY,own,fail,num,choice,date,iso,shift,month,weekdays,work,wholeMonths};\n})();const result=(function(v,h){\n    const {date,choice,DAY,wholeMonths,month,shift,work}=h;let a=date(v.startDate),b=date(v.endDate);if(+a>+b)[a,b]=[b,a];\n    choice(v.endpointPolicy,['excludeEnd','excludeStart','both']);const elapsed=Math.round((b-a)/DAY),days=elapsed+(v.endpointPolicy==='both'?1:0);\n    const first=v.endpointPolicy==='excludeStart'&&elapsed>0?shift(a,1):a,weekdays=work(first,days),completeMonths=wholeMonths(a,b),anchor=month(a,completeMonths),calendarDays=Math.round((b-anchor)/DAY);\n    const calendarYears=Math.floor(completeMonths/12),calendarMonths=completeMonths%12;\n    return {days,weeks:days/7,months:completeMonths,years:days/365.2425,weekdays,hours:days*24,minutes:days*1440,totalDays:days,totalHours:days*24,totalMinutes:days*1440,totalWeeks:days/7,businessDays:weekdays,completeMonths,calendarYears,calendarMonths,calendarDays,breakdown:calendarYears+' years, '+calendarMonths+' months, '+calendarDays+' days'};\n  })({\"startDate\":(startDate),\"endDate\":(endDate),\"endpointPolicy\":(endpointPolicy==='legacy'?(String(includeEndDay)==='1'?'both':String(includeEndDay)==='0'?'excludeEnd':'invalid'):endpointPolicy)},h);return result;};const out=calculate();return Object.fromEntries([\"days\",\"weeks\",\"months\",\"years\",\"weekdays\",\"hours\",\"calendarYears\",\"calendarMonths\",\"calendarDays\",\"minutes\"].map(k=>[k,out[k]??null]).concat(Object.entries(out).filter(([k])=>k.startsWith('__'))));}catch(error){return Object.assign(Object.fromEntries([\"days\",\"weeks\",\"months\",\"years\",\"weekdays\",\"hours\",\"calendarYears\",\"calendarMonths\",\"calendarDays\",\"minutes\"].map(k=>[k,null])),{__error:error.message==='openingsExceedWallArea'?'openingsExceedWallArea':'inputError'});}",
    "inputs": [
      {
        "name": "startDate",
        "type": "date",
        "default": "@today-1y",
        "step": 1,
        "preserveEmpty": true,
        "requiredValue": true
      },
      {
        "name": "endDate",
        "type": "date",
        "default": "@today",
        "step": 1,
        "preserveEmpty": true,
        "requiredValue": true
      },
      {
        "name": "includeEndDay",
        "type": "select",
        "default": 0,
        "options": [
          {
            "value": 0
          },
          {
            "value": 1
          }
        ],
        "showWhen": {
          "endpointPolicy": [
            "legacy"
          ]
        }
      },
      {
        "type": "select",
        "default": "legacy",
        "options": [
          {
            "value": "legacy"
          },
          {
            "value": "excludeEnd"
          },
          {
            "value": "excludeStart"
          },
          {
            "value": "both"
          }
        ],
        "advanced": true,
        "name": "endpointPolicy"
      }
    ],
    "outputs": [
      {
        "name": "days",
        "precision": 0,
        "unit": "days",
        "notation": "auto"
      },
      {
        "name": "weeks",
        "precision": 2,
        "unit": "weeks",
        "notation": "auto"
      },
      {
        "name": "months",
        "precision": 2,
        "unit": "months",
        "notation": "auto"
      },
      {
        "name": "years",
        "precision": 3,
        "unit": "years",
        "notation": "auto"
      },
      {
        "name": "weekdays",
        "precision": 0,
        "unit": "days",
        "notation": "auto"
      },
      {
        "name": "hours",
        "precision": 0,
        "unit": "h",
        "notation": "auto"
      },
      {
        "precision": 0,
        "unit": "",
        "name": "calendarYears",
        "notation": "auto"
      },
      {
        "precision": 0,
        "unit": "",
        "name": "calendarMonths",
        "notation": "auto"
      },
      {
        "precision": 0,
        "unit": "",
        "name": "calendarDays",
        "notation": "auto"
      },
      {
        "precision": 0,
        "unit": "min",
        "name": "minutes",
        "notation": "auto"
      }
    ],
    "interpret": null
  }
}
