ฟังก์ชัน getUTCDay() ของออบเจกต์ Date คืนค่า วันของสัปดาห์ (0-6) ของวันที่ปัจจุบันตามเวลาสากล (UTC) โดย 0 = อาทิตย์, 1 = จันทร์, ..., 6 = เสาร์
ไวยากรณ์
dateObj.getUTCDay();
ตัวอย่าง
<html> <head> <title>JavaScript Example</title> </head> <body> <script type="text/javascript"> var dateObj = new Date('september 26, 89 12:4:25:96'); document.write(dateObj.getUTCDay()); </script> </body> </html>
ผลลัพธ์
2
ตัวอย่าง: ไม่ระบุวันที่ (ใช้วันที่ 1 แทน)
<html> <head> <title>JavaScript Example</title> </head> <body> <script type="text/javascript"> var dateObj = new Date('September, 1989 00:4:00'); document.write(dateObj.getUTCDay()); </script> </body> </html>
ผลลัพธ์
4
ตัวอย่าง: ใช้วันที่ปัจจุบัน
<html> <head> <title>JavaScript Example</title> </head> <body> <script type="text/javascript"> var dateObj = new Date(); document.write(dateObj.getUTCDay()); </script> </body> </html>
ผลลัพธ์
4
ตารางอ้างอิงวันของสัปดาห์:
| ค่า | วัน |
|---|---|
| 0 | อาทิตย์ |
| 1 | จันทร์ |
| 2 | อังคาร |
| 3 | พุธ |
| 4 | พฤหัสบดี |
| 5 | ศุกร์ |
| 6 | เสาร์ |
การประยุกต์ใช้:
const days = ['อาทิตย์', 'จันทร์', 'อังคาร', 'พุธ', 'พฤหัสบดี', 'ศุกร์', 'เสาร์'];
const today = days[new Date().getUTCDay()];
console.log('วันนี้วัน' + today);