How to use getday in Javascript

By FoxLearn 9/5/2024 8:45:58 AM   73
To get the current day of the week in JavaScript, you can use the Date object.

How to use getday in Javascript

// Create a new Date object for the current date and time
const today = new Date();

// Get the day of the week as a number (0 for Sunday, 1 for Monday, etc.)
const dayIndex = today.getDay();

console.log(dayIndex);

Using new Date() to create a new Date object representing the current date and time.

Using getDay() to return the day of the week as a number from 0 (Sunday) to 6 (Saturday).

You can also use this approach to get the day of the week for any specific date by passing a date string or timestamp to the Date constructor.

For example:

const specificDate = new Date('2024-09-06'); // Example date
const dayIndex = specificDate.getDay();
const dayName = daysOfWeek[dayIndex];
console.log(dayName); // Outputs the day name for the specified date