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

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

ฟังก์ชัน setUTCSeconds() ของออบเจกต์ Date รับจำนวนเต็มแทนวินาที และแทนที่ค่าวินาทีในวันที่ปัจจุบันด้วยค่านั้น ตามเวลาสากล (UTC)

ไวยากรณ์

dateObj.setUTCSeconds(seconds);

ตัวอย่าง

<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.toUTCString());       document.write("<br>");       dateObj.setUTCSeconds(34);       document.write("วันที่หลังตั้งค่าวินาทีใหม่: "+dateObj.toUTCString());    </script> </body> </html>

ผลลัพธ์

วันที่ปัจจุบัน: Tue, 26 Sep 1989 06:34:25 GMT วันที่หลังตั้งค่าวินาทีใหม่: Tue, 26 Sep 1989 06:34:34 GMT

ตัวอย่าง: ตั้งค่าวินาทีโดยไม่ได้ระบูวินาทีตอนสร้าง

<html> <head>    <title>JavaScript Example</title> </head> <body>    <script type="text/javascript">       var dateObj = new Date('26, September, 1989 4:25');       dateObj.setSeconds(55);       document.write(dateObj.toString());    </script> </body> </html>

ผลลัพธ์

Tue Sep 26 1989 04:25:55 GMT+0530 (India Standard Time)

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

<html> <head>    <title>JavaScript Example</title> </head> <body>    <script type="text/javascript">       var dateObj = new Date();       dateObj.setSeconds(55);       document.write(dateObj.toString());    </script> </body> </html>

ผลลัพธ์

Thu Oct 18 2018 18:44:55 GMT+0530 (India Standard Time)

ข้อสังเกต:

  • ตัวอย่างที่ 2 และ 3 ใช้ setSeconds() (เวลาท้องถิ่น) แทน setUTCSeconds()
  • รองรับพารามิเตอร์ที่ 2: setUTCSeconds(seconds, ms)
  • ช่วงค่า: 0-59 (ค่าเกินจะเพิ่มนาทีอัตโนมัติ)
  • ค่าลบจะย้อนเวลากลับ