Javascript Anniversary Calculation

Date: 2022-06-21
const getDate = (year, month, day) => new Date(year, month-1, day);
const isLeapYear = (year) => ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0); 
const getDaysInMonth = (year, month) => [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
const editDate = (d, fn) => { 
    const nd = new Date(d);
    fn(nd);
    return nd;
};
const addYears = (date, years) => editDate(date, d => d.setFullYear(d.getFullYear() + years));
const addDays = (date, days) => editDate(date, d => d.setDate(d.getDate() + days));
const addMonths = function (date, value) {
    const newDate = new Date(date);
    var n = newDate.getDate();
    newDate.setDate(1);
    newDate.setMonth(newDate.getMonth() + value);
    newDate.setDate(Math.min(n, getDaysInMonth(newDate.getFullYear(), newDate.getMonth())));
    return newDate;
};
const date = getDate(2000, 8, 31);
const anniversary12 = addYears(date, 12);
let anniversary125 = addMonths(anniversary12, 6);
if (anniversary125.getDate() < date.getDate()) {
    anniversary125 = addDays(anniversary125, 1);
}
console.log(anniversary12, anniversary125)
// 1 March

C# Version

var date = newDateTime(2000, 8, 31);
var anniversary = date.AddYears(12).AddMonths(6);

if(anniversary.Day < date.Day) {
    anniversary = anniversary.AddDays(1);
} 

Console.WriteLine(anniversary);
// 1 March
63320cookie-checkJavascript Anniversary Calculation