Pretože definícia "vek v mesiacoch" je... flexibilné, najjednoduchší spôsob je použiť trochu aritmetický ako by ste počítať je vo vašej hlave, a nie zapojiť Date
triedy.
Pre [a] ľudské interpretácie "vek v mesiacoch", pravidlo sa
Výpočet rozdielu medzi dvoch termínoch v mesiacoch,
ako keby day-of-the-mesiac bol 1. mesiaca, za oba dátumy
Odrátame 1 vylúčiť posledný mesiac
Potom, ak day-of-the-mesiac posledný deň lehoty na
alebo po dni,-of-the-mesiaci, v prvý deň obdobia, na [potenciálne čiastočné] posledný mesiac je kompletné: pridať 1 do obnoviť počítať
Ten lietať v masť je, pretože mesiacov obsahovať rôzny počet dní, riešenie prípadov, keď 2 mesiace sa líšia v ich počet dní.
Ak však konci mesiaca je kratšia ako začať mesiac, môžete sa dostať do situácie, kde okrajová podmienka nemôže byť splnená (napr., dátum začiatku je 28. februára a dátum ukončenia je do 31. Marca. Ak chcete vyriešiť, budete musieť pozrieť na "koniec mesiaca" ako to, že okno v rozmedzí od posledného dňa začiatku mesiaca cez posledný deň konci mesiaca vrátane.
To vedie tohto kódexu. Ja používam štruktúru ako takto reprezentujú dátum:
{
year: 2021 , // 4-digit year
month: 11 , // month of year (1-12 mapping to January-December)
day: 23 // day of month (1-[28-31] depending on year/month
}
Zabezpečenie, aby údaje, ktoré struct predstavuje platný dátum je ponechaný ako cvičenie pre čitateľa.
Kód že to nie je zložité:
/**
*
* @param {object} bgn - start date of period
* @param {number} bgn.year - 4-digit year
* @param {number} bgn.month - month of year [1-12]
* @param {number} bgn.day - day of month [1-31]
*
* @param {object} end - end date of period
* @param {number} end.year - 4-digit year
* @param {number} end.month - month of year [1-12]
* @param {number} end.day - day of month [1-31]
*
*/
function diffInMonths( bgn , end ) {
const between = ( x , min , max ) => x >= min && x <= max;
// We'll need to add back the final month based on the following:
// - end.day >= bgn.day -- we've passed the month boundary, or
// - end.day is within the end-of-month window
// (when the end month is shorter than the start month)
const needAdjustment = end.day >= bgn.day
|| between( end.day, daysInMonth(bgn), daysInMonth(end) );
const finalMonthAdjustment = needsAdjustment ? 1 : 0;
const deltaM = 12 * ( end.year - bgn.year )
+ ( end.month - bgn.month )
- 1 // remove the final month from the equation
+ finalMonthAdjustment // add in the precomputed final month adjustment
;
return deltaM;
}
/**
*
* @param {object} dt - date
* @param {number} dt.year - 4-digit year
* @param {number} dt.month - month of year [1-12]
* @param {number} dt.day - day of month [1-31]
*
*/
function daysInMonth(dt) {
const leapYear = ( dt.year % 4 === 0 && dt.year % 100 !== 0 ) || dt.year % 400 === 0;
const monthDays = leapYear ? daysPerMonthLeap : daysPerMonth;
const days = monthDays[dt.month];
return days;
}
// jan feb mar apr may jun jul aug sep oct nov dec
// ---------- --- --- --- --- --- --- --- --- --- --- --- ---
const daysPerMonth = [ undefined, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, ];
const daysPerMonthLeap = [ undefined, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, ];
new Date(userDate.split('/').reverse())
takmer určite vrátiť neplatný Dátum.