JSFiddle - React, Tailwind, and code Playground
by spechackers
JavaScript
function isBDayInMonth(month, day) {
var today = new Date(),
birthDay = new Date(today.getFullYear(), month - 1, day, 0, 0, 0, 0); /*compose date*/
return (birthDay.getMonth() - today.getMonth() === 0) ? {
"msg": "this month",
"days": parseInt((birthDay.getTime() - today.getTime()) / 864E5, 10) === 0 ? "tomorrow" : parseInt((birthDay.getTime() - today.getTime()) / 864E5, 10) + " days remaining"
} : (birthDay.getMonth() - today.getMonth() === 1) ? {
"msg": "next month",
"days": parseInt((birthDay.getTime() - today.getTime()) / 864E5, 10) + " days remaining"
} : (today.getMonth() > birthDay.getMonth()) ? {
"msg": "in " + ((11 - today.getMonth()) + birthDay.getMonth()) + " months",
"days": "many days remaining"
} : {
"msg": "in " + (birthDay.getMonth() - today.getMonth()) + " months",
"days": "many days remaining"
};
}
var test = isBDayInMonth(2,20);
alert(test.msg + "\n" + test.days);
test = isBDayInMonth(11,20);
alert(test.msg + "\n" + test.days);