Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ฟังก์ชัน Date.getUTCMilliseconds() ใน JavaScript

ฟังก์ชัน getUTCMilliseconds() ของออบเจกต์ Date คืนค่า มิลลิวินาที (0-999) ของวันที่กำหนดตามเวลาสากล (UTC)

ไวยากรณ์

dateObj.getUTCMilliseconds();

ตัวอย่าง

<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.getUTCMilliseconds());    </script> </body> </html>

ผลลัพธ์

96

ตัวอย่าง: ไม่ระบุมิลลิวินาที

หากไม่ระบุมิลลิวินาทีตอนสร้าง จะคืนค่า 0 ตามเวลา UTC

<html> <head>    <title>JavaScript Example</title> </head> <body>    <script type="text/javascript">       var dateObj = new Date('September 26, 89 04:50:12');       document.write(dateObj.getUTCMilliseconds());    </script> </body> </html>

ผลลัพธ์

0

ตัวอย่าง: ใช้วันที่ปัจจุบัน

<html> <head>    <title>JavaScript Example</title> </head> <body>    <script type="text/javascript">       var dateObj = new Date();       document.write("มิลลิวินาทีปัจจุบัน: "+dateObj.getUTCMilliseconds());    </script> </body> </html>

ผลลัพธ์

มิลลิวินาทีปัจจุบัน: 226

ตัวอย่าง: ค่ามิลลิวินาทีเกินขอบเขต (0-999)

<html> <head>    <title>JavaScript Example</title> </head> <body>    <script type="text/javascript">       var dateObj = new Date('september 26, 89 04:50:12:1035');       document.write("มิลลิวินาทีปัจจุบัน: "+dateObj.getUTCMilliseconds());    </script> </body> </html>

ผลลัพธ์

มิลลิวินาทีปัจจุบัน: 0

สรุป:

  • ช่วงค่า: 0-999
  • ค่าเกิน 999 จะทำให้เวลาเลื่อนไป (แต่ getUTCMilliseconds() คืน 0)
  • ต่างจาก getMilliseconds() ที่ใช้เวลาท้องถิ่น
  • มีประโยชน์สำหรับการจับเวลาที่แม่นยำสูง (benchmarking, timestamps)