JSFiddle - React, Tailwind, and code Playground
by BladeBronson
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>
JavaScript
function shouldShowAnniversaryBalloon(startDateRaw, nowDateRaw) {
/** Days to show the anniversary balloon after the day of anniversary */
const DAYS_SHOW_BALLOON = 14;
const now = moment(nowDateRaw);
const startDate = moment(startDateRaw);
const years = now.diff(startDate, 'year');
startDate.add(years, 'years');
const months = now.diff(startDate, 'month');
startDate.add(months, 'months');
const days = now.diff(startDate, 'days');
console.log(startDateRaw, nowDateRaw, 'month_diff:', months, 'day_diff:', days, months === 0 && years > 0 && days >= 0 && days <= DAYS_SHOW_BALLOON);
return months === 0 && years > 0 && days >= 0 && days <= DAYS_SHOW_BALLOON;
}
// shouldShowAnniversaryBalloon('2019-03-01', '2020-03-14')
console.assert(shouldShowAnniversaryBalloon('2020-02-29', '2024-02-29')); // two leap days
console.assert(shouldShowAnniversaryBalloon('2020-02-29', '2021-03-01')); // day after leap day
console.assert(shouldShowAnniversaryBalloon('2020-02-29', '2021-03-14')); // 14 days after anniversary
console.assert(shouldShowAnniversaryBalloon('2020-02-29', '2034-03-14')); // 14 days after anniversary, many years later