ตั้งค่าวินาทีและมิลลิวินาทีเป็น 0 แล้วแปลง ISO String
ใช้ setSeconds(0, 0) ตั้งค่าวินาทีและมิลลิวินาที แล้ว toISOString()
ตัวอย่างโค้ด
var currentDate = new Date();
console.log("วันที่ปัจจุบัน = " + currentDate);
// ลบวินาทีและมิลลิวินาที (ตั้งเป็น 0)
currentDate.setSeconds(0, 0);
console.log("หลังลบวินาที = ");
console.log(currentDate.toISOString());
วิธีรัน
node fileName.js
ผลลัพธ์ตัวอย่าง
วันที่ปัจจุบัน = Sat Aug 01 2020 18:20:09 GMT+0530 (India Standard Time)
หลังลบวินาที = 2020-08-01T12:50:00.000Z
คำอธิบาย
setSeconds(0, 0): พารามิเตอร์ที่ 1 = วินาที, พารามิเตอร์ที่ 2 = มิลลิวินาทีtoISOString()คืนค่า ISO 8601 format (UTC) เช่น2020-08-01T12:50:00.000Z- Z ต่อท้าย = UTC timezone (Zulu time)
ฟังก์ชันสำเร็จรูป
function toISOWithoutSeconds(date = new Date()) {
const d = new Date(date);
d.setSeconds(0, 0);
return d.toISOString();
}
console.log(toISOWithoutSeconds()); // 2024-01-15T10:30:00.000Z