หากต้องการเปรียบเทียบวัตถุวันที่สองรายการกับ JavaScript ให้สร้างวัตถุวันที่สองรายการและรับวันที่ล่าสุดเพื่อเปรียบเทียบกับวันที่ที่กำหนดเอง
ตัวอย่าง
คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อเปรียบเทียบวันที่สองวัน -
<!DOCTYPE html>
<html>
<body>
<script>
var date1, date2;
date1 = new Date();
document.write(date1);
date2 = new Date( "Dec 10, 2015 20:15:10" );
document.write("<br>"+date2);
if (date1 > date2) {
document.write("<br>Date1 is the recent date.");
} else {
document.write("<br>Date 2 is the recent date.");
}
</script>
</body>
</html> ผลลัพธ์
Mon May 28 2018 09:48:49 GMT+0530 (India Standard Time) Thu Dec 10 2015 20:15:10 GMT+0530 (India Standard Time) Date1 is the recent date