2개의 날짜 개체를 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