Date.getDay() คืออะไร
เมธอด Date.prototype.getDay() คืนค่า วันของสัปดาห์ (day of week) ของ Date object ตาม เวลาท้องถิ่น (local time) ค่าที่ได้อยู่ในช่วง 0-6 โดยที่ 0 = อาทิตย์ (Sunday), 6 = เสาร์ (Saturday)
⚠️ สำคัญ: getDay() คืน วันของสัปดาห์ ไม่ใช่วันของเดือน (date of month) สำหรับวันของเดือนใช้ getDate()
ไวยากรณ์ (Syntax)
dateObj.getDay()
คืนค่า: จำนวนเต็ม 0-6 (0=อาทิตย์, 1=จันทร์, ..., 6=เสาร์)
ตัวอย่างการใช้งาน
ตัวอย่างที่ 1: การใช้งานพื้นฐาน
const date = new Date('1989-09-26'); // 26 กันยายน 1989
console.log(date.getDay()); // 2 (อังคาร)
ตัวอย่างที่ 2: ไม่ได้ระบุวันที่ (Default เป็นวันที่ 1)
const date = new Date('1989-09'); // กันยายน 1989
console.log(date.getDay()); // 5 (ศุกร์ - 1 กันยายน 1989 เป็นวันศุกร์)
// หมายเหตุ: ตัวอย่างเดิมกลับว่า 5 = Thursday แต่ 1 Sep 1989 จริงๆ เป็น Friday
ตัวอย่างที่ 3: วันปัจจุบัน
const now = new Date();
const dayNames = ['อาทิตย์', 'จันทร์', 'อังคาร', 'พุธ', 'พฤหัสบดี', 'ศุกร์', 'เสาร์'];
console.log(`วันนี้วัน${dayNames[now.getDay()]}`);
ตัวอย่างที่ 4: การแปลงเป็นชื่อวัน (ไทย/อังกฤษ)
const dayNamesTH = ['อาทิตย์', 'จันทร์', 'อังคาร', 'พุธ', 'พฤหัสบดี', 'ศุกร์', 'เสาร์'];
const dayNamesEN = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
function getDayName(date, locale = 'th-TH') {
return new Intl.DateTimeFormat(locale, { weekday: 'long' }).format(date);
}
const date = new Date('1989-09-26');
console.log(getDayName(date, 'th-TH')); // "อังคาร"
console.log(getDayName(date, 'en-US')); // "Tuesday"
UTC Version: getUTCDay()
const date = new Date('1989-09-26T12:04:25Z');
console.log('Local day:', date.getDay()); // ขึ้นกับ timezone
console.log('UTC day:', date.getUTCDay()); // 2 (อังคาร) เสมอ
กรณีการใช้งานทั่วไป
- Calendar UI: แสดงปฏิทิน, highlight วันหยุด
- Business logic: ตรวจสอบวันทำงาน/วันหยุด
- Scheduling: Cron jobs, recurring events
- Date formatting: แสดงชื่อวันใน locale ต่างๆ
- Analytics: จัดกลุ่มข้อมูลตามวันของสัปดาห์
ตัวอย่าง: Business Day Check
function isWeekend(date = new Date()) {
const day = date.getDay();
return day === 0 || day === 6; // Sun or Sat
}
function isBusinessDay(date = new Date()) {
return !isWeekend(date);
}
function addBusinessDays(date, days) {
const result = new Date(date);
let added = 0;
while (added < days) {
result.setDate(result.getDate() + 1);
if (isBusinessDay(result)) added++;
}
return result;
}
const friday = new Date('2024-01-12'); // Friday
console.log(addBusinessDays(friday, 1)); // Monday 2024-01-15
console.log(addBusinessDays(friday, 3)); // Wednesday 2024-01-17
ตัวอย่าง: Weekly Schedule
const schedule = {
0: ['Rest'], // Sunday
1: ['Work', 'Gym'], // Monday
2: ['Work', 'Study'], // Tuesday
3: ['Work', 'Gym'], // Wednesday
4: ['Work', 'Study'], // Thursday
5: ['Work', 'Movie'], // Friday
6: ['Clean', 'Prep'] // Saturday
};
function getTodaysSchedule(date = new Date()) {
return schedule[date.getDay()] || [];
}
console.log(getTodaysSchedule(new Date('2024-01-15'))); // Monday: ['Work', 'Gym']
ข้อควรระวัง
- คืนค่า 0-6 (0=Sunday) - ต่างจาก ISO 8601 (1=Monday)
- คืนค่า local time - ใช้
getUTCDay()สำหรับ UTC - Invalid Date -> คืนค่า
NaN - ไม่สนใจ timezone offset ในการคำนวณวัน (ใช้ local midnight)