JavaScript วันที่ setUTCHours()
ตัวอย่าง
ตั้งค่าชั่วโมงเป็น 15 ตามเวลา UTC:
const d = new Date();
d.setUTCHours(15);
ตัวอย่าง "ลองใช้เอง" เพิ่มเติมด้านล่าง
ความหมายและการใช้งาน
เมธอด setUTCHours() ตั้งค่าชั่วโมงของออบเจ็กต์วันที่ตาม UTC
นอกจากนี้ยังสามารถใช้ตั้งค่านาที วินาที และมิลลิวินาทีได้อีกด้วย
หมายเหตุ
UTC (Universal Time Coordinated) คือเวลาที่กำหนดโดยมาตรฐานเวลาโลก
เวลา UTC จะเหมือนกับเวลา GMT (เวลามาตรฐานกรีนิช)
รองรับเบราว์เซอร์
setUTCHours()
เป็นคุณลักษณะ ECMAScript1 (ES1)
ES1 (JavaScript 1997) ได้รับการสนับสนุนอย่างสมบูรณ์ในเบราว์เซอร์ทั้งหมด:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
ไวยากรณ์
Date.setUTCHours(hour, min, sec, millisec)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
hour | Required. An integer representing the hour.
Expected values are 0-23, but other values are allowed:
|
min | Optional. An integer representing the minutes.
Expected values are 0-59, but other values are allowed:
|
sec | Optional. An integer representing the seconds
Expected values are 0-59, but other values are allowed:
|
millisec | Optional. An integer representing the milliseconds
Expected values are 0-999, but other values are allowed:
|
รายละเอียดทางเทคนิค
คืนมูลค่า: | ตัวเลข แทนจำนวนมิลลิวินาทีระหว่างวัตถุวันที่จนถึงเที่ยงคืน 1 มกราคม 1970 |
---|---|
เวอร์ชันจาวาสคริปต์: | ECMAScript 1 |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
ตั้งเวลาเป็น 15:35:01 UTC time
const d = new Date();
d.setUTCHours(15, 35, 1);
ตัวอย่าง
ตั้งเวลาเป็น 48 ชั่วโมงที่แล้วโดยใช้วิธี UTC:
const d = new Date();
d.setUTCHours(d.getUTCHours() - 48);