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

วิธีเปรียบเทียบสองวันที่ใน JavaScript

เพื่อเปรียบเทียบสองวันที่ใน JavaScript ให้สร้างออบเจกต์ Date สองตัวและหาวันที่ที่ใหม่กว่า

ตัวอย่าง

<!DOCTYPE html>
<html>
  <body>
    <script>
      var date1, date2;
      date1 = new Date();
      document.write(date1);
      date2 = new Date("Dec 15, 2014 21:20:15");
      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>

ผลลัพธ์

Sat Dec 15 2018 11:08:06 GMT+0530 (India Standard Time) Mon Dec 15 2014 21:20:15 GMT+0530 (India Standard Time) Date1 is the recent date.