getDay()
ES1+Returns the day of the week (0-6) for the specified date according to local time.
Syntax
date.getDay()Return Value
A number (0-6) representing the day of the week (0 = Sunday)
Examples
const date = new Date('2024-01-15'); // μμμΌ
const days = ['μΌ', 'μ', 'ν', 'μ', 'λͺ©', 'κΈ', 'ν '];
console.log(date.getDay());
console.log(days[date.getDay()] + 'μμΌ'); π When to Use
Use getDay() for calendar rendering, business day calculations, weekend detection, or scheduling logic based on day of week.
β οΈ Common Mistakes
Assuming getDay() returns 1-7 like some other programming languages - JavaScript uses 0-6.
Forgetting that Sunday is 0, not 7, which can cause off-by-one errors in arrays.
β Best Practices
Check for weekends with: const isWeekend = date.getDay() === 0 || date.getDay() === 6.
For localized day names, use toLocaleString: date.toLocaleString(locale, { weekday: "long" }).
β‘ Performance Notes
getDay() is O(1). For repeated weekend checks in loops, inline the comparison rather than calling a function for marginal gains.
π Real World Example
Business Days Calculator
Calculate delivery date by adding business days, skipping weekends.
function addBusinessDays(startDate, daysToAdd) {
const result = new Date(startDate);
let addedDays = 0;
while (addedDays < daysToAdd) {
result.setDate(result.getDate() + 1);
const dayOfWeek = result.getDay();
// Skip weekends (0 = Sunday, 6 = Saturday)
if (dayOfWeek !== 0 && dayOfWeek !== 6) {
addedDays++;
}
}
return {
deliveryDate: result.toLocaleDateString(),
dayOfWeek: result.toLocaleString('en-US', { weekday: 'long' }),
businessDays: daysToAdd
};
}
console.log(addBusinessDays('2024-03-15', 5)); // Friday + 5 business days
// { deliveryDate: '3/22/2024', dayOfWeek: 'Friday', businessDays: 5 }