如何使用 JavaScript 檢查輸入日期是否等於今天的日期?
在本教程中,我們將學習如何使用 JavaScript 檢查輸入日期是否等於今天的日期。有時,我們需要以各種格式從輸入欄位中獲取使用者提供的日期,並檢查輸入日期和今天的日期是否匹配。
在這裡,我們將學習兩種方法來檢查輸入日期與今天的日期是否相等。
透過分別比較年份、月份和日期
使用者可以使用各種方法(例如 getFullYear()、getMonth() 和 getDate())從 Date 物件的時間戳中獲取完整的年份、月份和日期。此外,我們還可以透過使用分隔符分割使用者提供的字串來獲取年份、月份和日期。
之後,我們可以比較兩個時間戳的年份、月份和日期,以檢查輸入日期是否等於今天的日期。
語法
使用者可以按照以下語法來匹配輸入日期與今天的日期是否相等。
let inputYear = dateInput.value.split("/")[0];
let inputMonth = dateInput.value.split("/")[1];
let inputDate = dateInput.value.split("/")[2];
if (current_date.getFullYear() == inputYear && current_date.getMonth() == inputMonth - 1 && current_date.getDate(); == inputDate) {
// date is equal to today’s date
} else{
// date is not equal to today’s date
}
在上述語法中,我們透過“/”分割輸入字串後得到一個包含三個值的陣列。第一個值是年份,第二個是月份,第三個是日期。
演算法
使用者可以按照以下演算法操作。
步驟 1 - 以特定格式獲取使用者提供的日期。
步驟 2 - 分割日期字串並提取年份、月份和日期。這裡,我們使用了 JavaScript 的 split() 方法來分割日期字串。
步驟 3 - 使用 new Date() 建構函式建立一個等於今天的日期。
步驟 4 - 使用 getFullYear()、getMonth() 和 getDate() 方法從今天的日期中提取年份、月份和日期。
步驟 5 - 比較兩個時間戳的年份、月份和日期。getMonth() 方法返回 0 到 11 之間的月份。因此,我們需要將月份與 inputMonth – 1 進行比較。
步驟 6 - 如果兩個日期的所有三個屬性都匹配,則輸入日期等於今天的日期。
示例
在下面的示例中,我們建立了 compareDate() 函式。當用戶按下提交按鈕並在輸入欄位中輸入日期字串時,它會呼叫 compareDate() 函式。
compareDate() 函式實現了上述演算法,以檢查輸入日期是否等於今天的日期。
<html>
<body>
<h3>Comparing the <i>date, month, and year</i> separately to check if input the date is equal to today's date </h3>
<h4>Enter the date in yyyy/mm/dd format.</h4>
<input type="text" id="dateInput" />
<button id = "submit" onclick = "compareDate()"> submit </button>
<p id = "output"> </p>
<script>
let output = document.getElementById("output");
let dateInput = document.getElementById("dateInput");
function compareDate() {
// split the input string
let inputYear = dateInput.value.split("/")[0];
let inputMonth = dateInput.value.split("/")[1];
let inputDate = dateInput.value.split("/")[2];
let current_date = new Date();
// get the month, year, and date from the time stamp
let year = current_date.getFullYear();
let month = current_date.getMonth();
let day = current_date.getDate();
// compare year, month, and date
if (year == inputYear && month == inputMonth - 1 && day == inputDate) {
output.innerHTML = "Date is equal to today's date!";
} else {
output.innerHTML = "Date is not equal to today's date!";
}
}
</script>
</body>
</html>
使用 toDateString() 方法
我們可以將 toDateString() 方法與 Date 物件的時間戳一起使用,它僅從時間戳中返回日期字串。
我們可以使用從使用者輸入中獲取的任何值來建立一個新的 Date 物件時間戳,並使用 toDateString() 方法獲取日期字串。接下來,我們還可以建立表示今天日期的當前日期,並使用 toDateString() 方法。
最後,我們可以比較兩個時間戳的日期字串。
語法
使用者可以按照以下語法使用 toDateString() 方法來檢查輸入是否等於今天的日期。
let [year, month, date] = dateInput.value.split(",");
let inputDate = new Date(year, month - 1, date);
let current_date = new Date();
if (inputDate.toDateString() == current_date.toDateString()) {
// it’s today’s date.
} else {
// It’s not matching with today’s date
}
在上述語法中,我們使用年份、月份 – 1 和日期值建立了輸入日期的時間戳。由於 Date 物件對月份採用 0 到 11 之間的值,因此我們需要提供月份 – 1。
示例
以下示例以特定格式從使用者在文字輸入欄位中輸入的日期字串開始。之後,我們從字串中提取了年份、月份和日期,並建立了一個新日期。
接下來,我們比較了兩個時間戳的日期字串。
<html>
<body>
<h3>Using the <i> toDateString() </i> method to check if input the date is equal to today's date </h3>
<h4>Enter the date in yyyy,mm,dd format.</h4>
<input type = "text" id = "dateInput" />
<button id = "submit" onclick = "compareDate()"> submit </button>
<p id = "output"> </p>
<script>
let output = document.getElementById("output");
let dateInput = document.getElementById("dateInput");
function compareDate() {
// extract the year, month, and date
let [year, month, date] = dateInput.value.split(",");
let inputDate = new Date(year, month - 1, date);
let current_date = new Date();
if (inputDate.toDateString() == current_date.toDateString()) {
output.innerHTML = "Date is equal to today's date!";
} else {
output.innerHTML = "Date is not equal to today's date!";
}
}
</script>
</body>
</html>
在上述輸出中,使用者可以觀察到,以無效格式輸入日期將在控制檯中生成錯誤。
使用者學習了兩種方法來檢查日期是否等於今天的日期。但是,使用者也可以使用 setHours() 方法。我們可以在日期物件中將小時設定為 0,並將其與今天的日期進行比較以檢查相等性。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP