Date.getTimezoneOffset() คืออะไร
เมธอด Date.prototype.getTimezoneOffset() คืนค่า ความแตกต่างของโซนเวลา (timezone offset) ระหว่างเวลาท้องถิ่น (local time) และ UTC ในหน่วย นาที (minutes)
ค่าที่คืนมาเป็น บวก (+) หากเวลาท้องถิ่น อยู่หลัง UTC (เช่น เวลาฝรั่งเศส UTC+1 -> -60) และเป็น ลบ (-) หากเวลาท้องถิ่น อยู่หน้า UTC (เช่น เวลาไทย UTC+7 -> -420)
สำคัญ: เครื่องหมาย (sign) เป็นตรงข้ามกับที่คาดหวัง!
- UTC+7 (ไทย) -> getTimezoneOffset() = -420
- UTC-5 (EST) -> getTimezoneOffset() = 300
- UTC+0 (GMT) -> getTimezoneOffset() = 0
ไวยากรณ์ (Syntax)
dateObj.getTimezoneOffset()
คืนค่า: จำนวนเต็มแทน offset ในนาที (อาจเป็นบวกหรือลบ)
ตัวอย่างการใช้งาน
ตัวอย่างที่ 1: การใช้งานพื้นฐาน
// สมมติรันใน timezone UTC+7 (ไทย)
const date = new Date('1989-09-26T12:04:25');
console.log(date.getTimezoneOffset()); // -420 (7 ชั่วโมง * 60 นาที)
// สมมติรันใน timezone UTC-5 (EST)
const date2 = new Date('1989-09-26T12:04:25');
console.log(date2.getTimezoneOffset()); // 300 (5 ชั่วโมง * 60 นาที)
ตัวอย่างที่ 2: การคำนวณ UTC จาก Local Time
const localDate = new Date(); // เวลาท้องถิ่นปัจจุบัน
const offsetMinutes = localDate.getTimezoneOffset(); // เช่น -420 สำหรับไทย
// แปลงเป็น UTC
const utcDate = new Date(localDate.getTime() + offsetMinutes * 60000);
console.log('Local:', localDate.toString());
console.log('UTC:', utcDate.toUTCString());
ตัวอย่างที่ 3: การตรวจสอบ Daylight Saving Time (DST)
function isDST(date = new Date()) {
const jan = new Date(date.getFullYear(), 0, 1);
const jul = new Date(date.getFullYear(), 6, 1);
const stdOffset = Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
return date.getTimezoneOffset() < stdOffset;
}
// ทดสอบ
const summer = new Date('2024-07-15');
const winter = new Date('2024-01-15');
console.log('July DST:', isDST(summer)); // true (ในเขตที่มี DST)
console.log('Jan DST:', isDST(winter)); // false
ตัวอย่างที่ 4: วันที่ไม่ถูกต้อง (Invalid Date)
const invalidDate = new Date('September 40, 1989');
console.log(invalidDate.getTimezoneOffset()); // NaN
console.log(invalidDate.toString()); // "Invalid Date"
Timezone Offset vs Timezone
| Timezone | Offset from UTC | getTimezoneOffset() |
|---|---|---|
| UTC+12 (NZST) | +12 hours | -720 |
| UTC+9 (JST/KST) | +9 hours | -540 |
| UTC+8 (CST) | +8 hours | -480 |
| UTC+7 (ICT/Thai) | +7 hours | -420 |
| UTC+5:30 (IST) | +5.5 hours | -330 |
| UTC+0 (GMT/UTC) | 0 | 0 |
| UTC-5 (EST) | -5 hours | 300 |
| UTC-8 (PST) | -8 hours | 480 |
กรณีการใช้งานทั่วไป
- Timezone detection: ตรวจสอบ timezone ของ user
- UTC conversion: แปลง local time เป็น UTC และกลับกัน
- DST detection: ตรวจสอบว่าอยู่ในช่วง daylight saving หรือไม่
- Scheduling: คำนวณเวลา meeting ระหว่าง timezone
- Logging: บันทึก offset พร้อม timestamp เพื่อ debug
ข้อควรระวัง
- เครื่องหมาย相反: UTC+7 ให้ -420, UTC-5 ให้ +300
- ขึ้นอยู่กับ system timezone setting ของเครื่องที่รันโค้ด
- คืนค่า
NaNหาก Date object เป็น Invalid Date - อาจเปลี่ยนค่าได้ตลอดปี หากในเขตที่มี DST
- ไม่ได้บอก timezone name (เช่น "Asia/Bangkok") - ใช้
Intl.DateTimeFormat().resolvedOptions().timeZoneแทน
Modern Alternative: Intl API
// ดึง timezone name (IANA timezone)
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
console.log(timeZone); // "Asia/Bangkok", "America/New_York", etc.
// ดึง offset ในรูปแบบที่อ่านง่าย
const now = new Date();
const formatter = new Intl.DateTimeFormat('en-US', {
timeZone: timeZone,
timeZoneName: 'short'
});
console.log(formatter.format(now)); // "1/15/2024, 5:30 PM GMT+7"