what is my birthday in javascript

To calculate and display your birthday in JavaScript, you can use the Date() object and its methods to store, manipulate and format the date. Here's an example code that takes the user input (their birth year, month and day) and calculates and displays their birthday as a formatted string:

index.tsx
// prompt user for birth date input
const year = parseInt(prompt('Enter your birth year: '), 10);
const month = parseInt(prompt('Enter your birth month (1-12): '), 10) - 1; // months are zero-indexed
const day = parseInt(prompt('Enter your birth day: '), 10);

// create a new Date object with the user's birthdate
const birthday = new Date(year, month, day);

// format the date as a string using the toLocaleDateString() method
const birthdayString = birthday.toLocaleDateString('en-US', {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
});

// display the result
console.log(`Your birthday is ${birthdayString}`);
643 chars
19 lines

This code prompts the user for their birthdate input, creates a new Date object with the given values, formats it as a string using the toLocaleDateString() method, and displays the result in the console.

You can of course modify this code to fit your needs, such as by displaying the result on the webpage instead of in the console.

related categories

gistlibby LogSnag