[{"data":1,"prerenderedAt":709},["ShallowReactive",2],{"guide-date-difference-calculation":3},{"id":4,"title":5,"body":6,"date":701,"description":702,"extension":703,"meta":704,"navigation":242,"path":705,"readingTime":265,"seo":706,"stem":707,"__hash__":708},"guides\u002Fguides\u002Fdate-difference-calculation.md","How to Calculate Date Differences",{"type":7,"value":8,"toc":690},"minimark",[9,14,18,22,25,30,86,90,93,106,391,395,398,472,479,483,486,636,639,643,673,677,686],[10,11,13],"h2",{"id":12},"why-date-differences-matter","Why Date Differences Matter",[15,16,17],"p",{},"Calculating the time between two dates comes up constantly — project deadlines, age verification, subscription billing, and legal compliance all depend on precise date math. Getting it wrong can cause billing errors or regulatory violations.",[10,19,21],{"id":20},"the-challenge-with-months","The Challenge with Months",[15,23,24],{},"Days are fixed units, but months vary from 28 to 31 days. This makes \"1 month ago\" ambiguous without context. February 28 minus one month could reasonably be January 28 or January 31, depending on the convention.",[26,27,29],"h3",{"id":28},"common-approaches","Common Approaches",[31,32,33,49],"table",{},[34,35,36],"thead",{},[37,38,39,43,46],"tr",{},[40,41,42],"th",{},"Approach",[40,44,45],{},"How It Works",[40,47,48],{},"Best For",[50,51,52,64,75],"tbody",{},[37,53,54,58,61],{},[55,56,57],"td",{},"Day count",[55,59,60],{},"Count total days between dates",[55,62,63],{},"Short durations, deadlines",[37,65,66,69,72],{},[55,67,68],{},"Calendar months",[55,70,71],{},"Increment\u002Fdecrement months, adjust days",[55,73,74],{},"Age, billing, contracts",[37,76,77,80,83],{},[55,78,79],{},"30-day month",[55,81,82],{},"Assume 30 days per month",[55,84,85],{},"Financial calculations",[10,87,89],{"id":88},"calculating-age-from-date-of-birth","Calculating Age from Date of Birth",[15,91,92],{},"Age calculation requires calendar-month logic, not simple division:",[94,95,96,100,103],"ol",{},[97,98,99],"li",{},"Subtract birth year from current year",[97,101,102],{},"If the current month-day is before the birth month-day, subtract 1",[97,104,105],{},"Handle February 29 births in non-leap years",[107,108,113],"pre",{"className":109,"code":110,"language":111,"meta":112,"style":112},"language-javascript shiki shiki-themes github-light github-dark","function calculateAge(birthDate, referenceDate = new Date()) {\n  let years = referenceDate.getFullYear() - birthDate.getFullYear()\n  let months = referenceDate.getMonth() - birthDate.getMonth()\n  let days = referenceDate.getDate() - birthDate.getDate()\n\n  if (days \u003C 0) {\n    months--\n    const prevMonth = new Date(referenceDate.getFullYear(), referenceDate.getMonth(), 0)\n    days += prevMonth.getDate()\n  }\n\n  if (months \u003C 0) {\n    years--\n    months += 12\n  }\n\n  return { years, months, days }\n}\n","javascript","",[114,115,116,155,187,212,237,244,263,272,306,322,328,333,347,355,366,371,376,385],"code",{"__ignoreMap":112},[117,118,121,125,129,133,137,140,143,146,149,152],"span",{"class":119,"line":120},"line",1,[117,122,124],{"class":123},"szBVR","function",[117,126,128],{"class":127},"sScJk"," calculateAge",[117,130,132],{"class":131},"sVt8B","(",[117,134,136],{"class":135},"s4XuR","birthDate",[117,138,139],{"class":131},", ",[117,141,142],{"class":135},"referenceDate",[117,144,145],{"class":123}," =",[117,147,148],{"class":123}," new",[117,150,151],{"class":127}," Date",[117,153,154],{"class":131},"()) {\n",[117,156,158,161,164,167,170,173,176,179,182,184],{"class":119,"line":157},2,[117,159,160],{"class":123},"  let",[117,162,163],{"class":131}," years ",[117,165,166],{"class":123},"=",[117,168,169],{"class":131}," referenceDate.",[117,171,172],{"class":127},"getFullYear",[117,174,175],{"class":131},"() ",[117,177,178],{"class":123},"-",[117,180,181],{"class":131}," birthDate.",[117,183,172],{"class":127},[117,185,186],{"class":131},"()\n",[117,188,190,192,195,197,199,202,204,206,208,210],{"class":119,"line":189},3,[117,191,160],{"class":123},[117,193,194],{"class":131}," months ",[117,196,166],{"class":123},[117,198,169],{"class":131},[117,200,201],{"class":127},"getMonth",[117,203,175],{"class":131},[117,205,178],{"class":123},[117,207,181],{"class":131},[117,209,201],{"class":127},[117,211,186],{"class":131},[117,213,215,217,220,222,224,227,229,231,233,235],{"class":119,"line":214},4,[117,216,160],{"class":123},[117,218,219],{"class":131}," days ",[117,221,166],{"class":123},[117,223,169],{"class":131},[117,225,226],{"class":127},"getDate",[117,228,175],{"class":131},[117,230,178],{"class":123},[117,232,181],{"class":131},[117,234,226],{"class":127},[117,236,186],{"class":131},[117,238,240],{"class":119,"line":239},5,[117,241,243],{"emptyLinePlaceholder":242},true,"\n",[117,245,247,250,253,256,260],{"class":119,"line":246},6,[117,248,249],{"class":123},"  if",[117,251,252],{"class":131}," (days ",[117,254,255],{"class":123},"\u003C",[117,257,259],{"class":258},"sj4cs"," 0",[117,261,262],{"class":131},") {\n",[117,264,266,269],{"class":119,"line":265},7,[117,267,268],{"class":131},"    months",[117,270,271],{"class":123},"--\n",[117,273,275,278,281,283,285,287,290,292,295,297,300,303],{"class":119,"line":274},8,[117,276,277],{"class":123},"    const",[117,279,280],{"class":258}," prevMonth",[117,282,145],{"class":123},[117,284,148],{"class":123},[117,286,151],{"class":127},[117,288,289],{"class":131},"(referenceDate.",[117,291,172],{"class":127},[117,293,294],{"class":131},"(), referenceDate.",[117,296,201],{"class":127},[117,298,299],{"class":131},"(), ",[117,301,302],{"class":258},"0",[117,304,305],{"class":131},")\n",[117,307,309,312,315,318,320],{"class":119,"line":308},9,[117,310,311],{"class":131},"    days ",[117,313,314],{"class":123},"+=",[117,316,317],{"class":131}," prevMonth.",[117,319,226],{"class":127},[117,321,186],{"class":131},[117,323,325],{"class":119,"line":324},10,[117,326,327],{"class":131},"  }\n",[117,329,331],{"class":119,"line":330},11,[117,332,243],{"emptyLinePlaceholder":242},[117,334,336,338,341,343,345],{"class":119,"line":335},12,[117,337,249],{"class":123},[117,339,340],{"class":131}," (months ",[117,342,255],{"class":123},[117,344,259],{"class":258},[117,346,262],{"class":131},[117,348,350,353],{"class":119,"line":349},13,[117,351,352],{"class":131},"    years",[117,354,271],{"class":123},[117,356,358,361,363],{"class":119,"line":357},14,[117,359,360],{"class":131},"    months ",[117,362,314],{"class":123},[117,364,365],{"class":258}," 12\n",[117,367,369],{"class":119,"line":368},15,[117,370,327],{"class":131},[117,372,374],{"class":119,"line":373},16,[117,375,243],{"emptyLinePlaceholder":242},[117,377,379,382],{"class":119,"line":378},17,[117,380,381],{"class":123},"  return",[117,383,384],{"class":131}," { years, months, days }\n",[117,386,388],{"class":119,"line":387},18,[117,389,390],{"class":131},"}\n",[10,392,394],{"id":393},"computing-days-between-two-dates","Computing Days Between Two Dates",[15,396,397],{},"For simple day counts, use the difference in milliseconds:",[107,399,401],{"className":109,"code":400,"language":111,"meta":112,"style":112},"const daysBetween = (start, end) =>\n  Math.floor((end - start) \u002F (1000 * 60 * 60 * 24))\n",[114,402,403,430],{"__ignoreMap":112},[117,404,405,408,411,413,416,419,421,424,427],{"class":119,"line":120},[117,406,407],{"class":123},"const",[117,409,410],{"class":127}," daysBetween",[117,412,145],{"class":123},[117,414,415],{"class":131}," (",[117,417,418],{"class":135},"start",[117,420,139],{"class":131},[117,422,423],{"class":135},"end",[117,425,426],{"class":131},") ",[117,428,429],{"class":123},"=>\n",[117,431,432,435,438,441,443,446,449,451,454,457,460,462,464,466,469],{"class":119,"line":157},[117,433,434],{"class":131},"  Math.",[117,436,437],{"class":127},"floor",[117,439,440],{"class":131},"((end ",[117,442,178],{"class":123},[117,444,445],{"class":131}," start) ",[117,447,448],{"class":123},"\u002F",[117,450,415],{"class":131},[117,452,453],{"class":258},"1000",[117,455,456],{"class":123}," *",[117,458,459],{"class":258}," 60",[117,461,456],{"class":123},[117,463,459],{"class":258},[117,465,456],{"class":123},[117,467,468],{"class":258}," 24",[117,470,471],{"class":131},"))\n",[15,473,474,475,478],{},"This works reliably because JavaScript Date subtraction returns milliseconds. Always use ",[114,476,477],{},"Math.floor"," to avoid fractional days from time components.",[10,480,482],{"id":481},"business-days-calculation","Business Days Calculation",[15,484,485],{},"Excluding weekends requires iterating through the date range:",[107,487,489],{"className":109,"code":488,"language":111,"meta":112,"style":112},"function businessDaysBetween(start, end) {\n  let count = 0\n  const current = new Date(start)\n  while (current \u003C= end) {\n    const day = current.getDay()\n    if (day !== 0 && day !== 6) count++\n    current.setDate(current.getDate() + 1)\n  }\n  return count\n}\n",[114,490,491,508,520,537,551,568,598,621,625,632],{"__ignoreMap":112},[117,492,493,495,498,500,502,504,506],{"class":119,"line":120},[117,494,124],{"class":123},[117,496,497],{"class":127}," businessDaysBetween",[117,499,132],{"class":131},[117,501,418],{"class":135},[117,503,139],{"class":131},[117,505,423],{"class":135},[117,507,262],{"class":131},[117,509,510,512,515,517],{"class":119,"line":157},[117,511,160],{"class":123},[117,513,514],{"class":131}," count ",[117,516,166],{"class":123},[117,518,519],{"class":258}," 0\n",[117,521,522,525,528,530,532,534],{"class":119,"line":189},[117,523,524],{"class":123},"  const",[117,526,527],{"class":258}," current",[117,529,145],{"class":123},[117,531,148],{"class":123},[117,533,151],{"class":127},[117,535,536],{"class":131},"(start)\n",[117,538,539,542,545,548],{"class":119,"line":214},[117,540,541],{"class":123},"  while",[117,543,544],{"class":131}," (current ",[117,546,547],{"class":123},"\u003C=",[117,549,550],{"class":131}," end) {\n",[117,552,553,555,558,560,563,566],{"class":119,"line":239},[117,554,277],{"class":123},[117,556,557],{"class":258}," day",[117,559,145],{"class":123},[117,561,562],{"class":131}," current.",[117,564,565],{"class":127},"getDay",[117,567,186],{"class":131},[117,569,570,573,576,579,581,584,587,589,592,595],{"class":119,"line":246},[117,571,572],{"class":123},"    if",[117,574,575],{"class":131}," (day ",[117,577,578],{"class":123},"!==",[117,580,259],{"class":258},[117,582,583],{"class":123}," &&",[117,585,586],{"class":131}," day ",[117,588,578],{"class":123},[117,590,591],{"class":258}," 6",[117,593,594],{"class":131},") count",[117,596,597],{"class":123},"++\n",[117,599,600,603,606,609,611,613,616,619],{"class":119,"line":265},[117,601,602],{"class":131},"    current.",[117,604,605],{"class":127},"setDate",[117,607,608],{"class":131},"(current.",[117,610,226],{"class":127},[117,612,175],{"class":131},[117,614,615],{"class":123},"+",[117,617,618],{"class":258}," 1",[117,620,305],{"class":131},[117,622,623],{"class":119,"line":274},[117,624,327],{"class":131},[117,626,627,629],{"class":119,"line":308},[117,628,381],{"class":123},[117,630,631],{"class":131}," count\n",[117,633,634],{"class":119,"line":324},[117,635,390],{"class":131},[15,637,638],{},"For production use, also subtract public holidays by maintaining a holiday calendar.",[10,640,642],{"id":641},"edge-cases-to-watch","Edge Cases to Watch",[644,645,646,661,667],"ul",{},[97,647,648,652,653,656,657,660],{},[649,650,651],"strong",{},"Timezone differences",": Dates created as ",[114,654,655],{},"new Date(\"2024-01-01\")"," are UTC, while ",[114,658,659],{},"new Date(2024, 0, 1)"," is local. Mixing them causes off-by-one errors.",[97,662,663,666],{},[649,664,665],{},"Daylight saving time",": A day is not always 24 hours near DST transitions. Use date-only arithmetic, not hour-based math.",[97,668,669,672],{},[649,670,671],{},"Leap seconds",": Standard JavaScript does not handle leap seconds. For most applications, this is irrelevant.",[10,674,676],{"id":675},"try-it-yourself","Try It Yourself",[15,678,679,680,685],{},"Use the ",[681,682,684],"a",{"href":683},"\u002Ftools\u002Fage-calculator","Age Calculator"," to compute exact age in years, months, and days with all edge cases handled automatically.",[687,688,689],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":112,"searchDepth":157,"depth":157,"links":691},[692,693,696,697,698,699,700],{"id":12,"depth":157,"text":13},{"id":20,"depth":157,"text":21,"children":694},[695],{"id":28,"depth":189,"text":29},{"id":88,"depth":157,"text":89},{"id":393,"depth":157,"text":394},{"id":481,"depth":157,"text":482},{"id":641,"depth":157,"text":642},{"id":675,"depth":157,"text":676},"2026-05-28","Learn accurate methods to calculate the difference between two dates in days, months, and years.","md",{},"\u002Fguides\u002Fdate-difference-calculation",{"title":5,"description":702},"guides\u002Fdate-difference-calculation","uD6Oaz-r7u3OzgFVMs60XEEkvMqx_map4Qf37FsTbP8",1780401333351]