check if day is birthday in javascript

To check if the current day is someone's birthday in JavaScript, you can use the built-in Date object to get today's date, create a new Date object with the person's birthdate, and then compare them to see if they are the same.

Here's an example code block that checks if today is someone's birthday:

index.tsx
// Get today's date
const today = new Date();

// Create a new date object with the person's birthdate
const birthdate = new Date('1990-01-01');

// Compare the month and day of today's date and the birthdate
if (today.getMonth() === birthdate.getMonth() && today.getDate() === birthdate.getDate()) {
  console.log("It's their birthday!");
} else {
  console.log("Sorry, today is not their birthday.");
}
405 chars
13 lines

In this example, we're checking if the current month and day (getMonth() and getDate()) of today's date match the birthdate passed to the Date object. If they do match, we log a message saying it's their birthday. If they don't match, we log a message saying it's not their birthday.

gistlibby LogSnag