Ngày là loại dữ liệu thực sự phổ biến mà các nhà phát triển làm việc với. Từ dấu thời gian của một số hành động nhất định đến báo cáo, tính năng đăng ký và quyền truy cập trong thời gian giới hạn trong các hệ thống yêu cầu đăng ký - đôi khi chúng tôi phải so sánh ngày tháng
Nghĩa là, chúng ta so sánh xem một ngày là sau hay trước một ngày khác, nếu là ngày hôm nay, thì có bao nhiêu ngày giữa các ngày, v.v.
Trong bài viết này, chúng ta sẽ xem xét cách so sánh hai ngày trong JavaScript, giúp chúng ta suy luận xem một ngày là trước hay sau một ngày khác
Các nhà phát triển web thường sử dụng các gói bên ngoài [như Moment. js] để xử lý các hoạt động ngày giờ. Tuy nhiên, khi web hiện đại phát triển, JavaScript đã giới thiệu một hàm tạo đối tượng mới có tên là
function dateCompare[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1 > date2]{
console.log[`${d1} is greater than ${d2}`]
} else if[date1 < date2]{
console.log[`${d2} is greater than ${d1}`]
} else{
console.log[`Both dates are equal`]
}
}
dateCompare["6/11/2020", "7/8/2019"]
dateCompare["01/01/2021", "01/01/2021"]
6 để xử lý các hoạt động ngày giờĐiều này có nghĩa là bạn không cần một thư viện bên ngoài để thực hiện các thao tác và kiểm tra thô sơ, điều này giúp thực hiện những việc này dễ dàng hơn trong Vanilla JS
Lớp
function dateCompare[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1 > date2]{
console.log[`${d1} is greater than ${d2}`]
} else if[date1 < date2]{
console.log[`${d2} is greater than ${d1}`]
} else{
console.log[`Both dates are equal`]
}
}
dateCompare["6/11/2020", "7/8/2019"]
dateCompare["01/01/2021", "01/01/2021"]
6 thực sự dễ hiểu - nó chỉ lưu trữ thời gian Unix được tính bằng mili giâyThời gian Unix được đo bằng số giây trôi qua kể từ kỷ nguyên Unix [00. 00. 00 UTC ngày 1 tháng 1 năm 1970], đây là một ngày hoàn toàn tùy ý
Mặc dù việc triển khai này có vẻ hơi đơn giản, nhưng việc bổ sung lớp
function dateCompare[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1 > date2]{
console.log[`${d1} is greater than ${d2}`]
} else if[date1 < date2]{
console.log[`${d2} is greater than ${d1}`]
} else{
console.log[`Both dates are equal`]
}
}
dateCompare["6/11/2020", "7/8/2019"]
dateCompare["01/01/2021", "01/01/2021"]
6 là một cải tiến khá lớn, vì cuối cùng đã có một mức độ trừu tượng giữa nhà phát triển và ngày thôBây giờ, hãy xem các cách khác nhau để so sánh hai ngày bằng cách sử dụng đối tượng
function dateCompare[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1 > date2]{
console.log[`${d1} is greater than ${d2}`]
} else if[date1 < date2]{
console.log[`${d2} is greater than ${d1}`]
} else{
console.log[`Both dates are equal`]
}
}
dateCompare["6/11/2020", "7/8/2019"]
dateCompare["01/01/2021", "01/01/2021"]
6Chúng ta có thể sử dụng các toán tử so sánh như
6/11/2020 is greater than 7/8/2019
Both dates are equal
0 và 6/11/2020 is greater than 7/8/2019
Both dates are equal
1 hai so sánh hai đối tượng function dateCompare[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1 > date2]{
console.log[`${d1} is greater than ${d2}`]
} else if[date1 < date2]{
console.log[`${d2} is greater than ${d1}`]
} else{
console.log[`Both dates are equal`]
}
}
dateCompare["6/11/2020", "7/8/2019"]
dateCompare["01/01/2021", "01/01/2021"]
6 và dưới mui xe, bộ đếm thời gian của chúng được so sánh một cách hiệu quả. Bạn đang so sánh hiệu quả hai bộ đếm số nguyênfunction dateCompare[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1 > date2]{
console.log[`${d1} is greater than ${d2}`]
} else if[date1 < date2]{
console.log[`${d2} is greater than ${d1}`]
} else{
console.log[`Both dates are equal`]
}
}
dateCompare["6/11/2020", "7/8/2019"]
dateCompare["01/01/2021", "01/01/2021"]
Kết quả này trong
6/11/2020 is greater than 7/8/2019
Both dates are equal
Như chúng ta có thể thấy, việc so sánh ngày tháng chỉ đơn giản là chuyển đổi các chuỗi được cung cấp thành các đối tượng
function dateCompare[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1 > date2]{
console.log[`${d1} is greater than ${d2}`]
} else if[date1 < date2]{
console.log[`${d2} is greater than ${d1}`]
} else{
console.log[`Both dates are equal`]
}
}
dateCompare["6/11/2020", "7/8/2019"]
dateCompare["01/01/2021", "01/01/2021"]
6 và so sánh chúng với một toán tử so sánh thích hợpGhi chú. Các toán tử đẳng thức [
6/11/2020 is greater than 7/8/2019
Both dates are equal
4 và 6/11/2020 is greater than 7/8/2019
Both dates are equal
5] không hoạt động với các đối tượng function dateCompare[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1 > date2]{
console.log[`${d1} is greater than ${d2}`]
} else if[date1 < date2]{
console.log[`${d2} is greater than ${d1}`]
} else{
console.log[`Both dates are equal`]
}
}
dateCompare["6/11/2020", "7/8/2019"]
dateCompare["01/01/2021", "01/01/2021"]
6, vì vậy chúng tôi không kiểm tra rõ ràng xem chúng có giống nhau khôngMột cách khác để so sánh hai ngày là sử dụng phương pháp tích hợp sẵn
6/11/2020 is greater than 7/8/2019
Both dates are equal
7Phương thức
6/11/2020 is greater than 7/8/2019
Both dates are equal
7 trả về số mili giây đã trôi qua kể từ kỷ nguyên Unix. Ngoài ra, bạn có thể sử dụng các phương pháp, 6/11/2020 is greater than 7/8/2019
Both dates are equal
9, function compareDates[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1.getTime[] > date2.getTime[]]{
console.log[`${d1} is greater than ${d2} in terms of milliseconds`]
} else if[date1.getYear[] < date2.getYear[]]{
console.log[`${d2} is greater than ${d1} in terms of years`]
} else if[date1.getDate[] === date2.getDate[]]{
console.log[`Both dates are equal`]
}
}
compareDates["9/10/1997", "9/10/2000"]
compareDates["11/11/2021", "11/1/2021"]
0, function compareDates[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1.getTime[] > date2.getTime[]]{
console.log[`${d1} is greater than ${d2} in terms of milliseconds`]
} else if[date1.getYear[] < date2.getYear[]]{
console.log[`${d2} is greater than ${d1} in terms of years`]
} else if[date1.getDate[] === date2.getDate[]]{
console.log[`Both dates are equal`]
}
}
compareDates["9/10/1997", "9/10/2000"]
compareDates["11/11/2021", "11/1/2021"]
1, function compareDates[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1.getTime[] > date2.getTime[]]{
console.log[`${d1} is greater than ${d2} in terms of milliseconds`]
} else if[date1.getYear[] < date2.getYear[]]{
console.log[`${d2} is greater than ${d1} in terms of years`]
} else if[date1.getDate[] === date2.getDate[]]{
console.log[`Both dates are equal`]
}
}
compareDates["9/10/1997", "9/10/2000"]
compareDates["11/11/2021", "11/1/2021"]
2 và function compareDates[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1.getTime[] > date2.getTime[]]{
console.log[`${d1} is greater than ${d2} in terms of milliseconds`]
} else if[date1.getYear[] < date2.getYear[]]{
console.log[`${d2} is greater than ${d1} in terms of years`]
} else if[date1.getDate[] === date2.getDate[]]{
console.log[`Both dates are equal`]
}
}
compareDates["9/10/1997", "9/10/2000"]
compareDates["11/11/2021", "11/1/2021"]
3 để xác định rõ hơn và so sánh thông tin, trong số các phương pháp có tên tương tự khácNgoài ra, bạn cũng có thể sử dụng
function compareDates[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1.getTime[] > date2.getTime[]]{
console.log[`${d1} is greater than ${d2} in terms of milliseconds`]
} else if[date1.getYear[] < date2.getYear[]]{
console.log[`${d2} is greater than ${d1} in terms of years`]
} else if[date1.getDate[] === date2.getDate[]]{
console.log[`Both dates are equal`]
}
}
compareDates["9/10/1997", "9/10/2000"]
compareDates["11/11/2021", "11/1/2021"]
4, function compareDates[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1.getTime[] > date2.getTime[]]{
console.log[`${d1} is greater than ${d2} in terms of milliseconds`]
} else if[date1.getYear[] < date2.getYear[]]{
console.log[`${d2} is greater than ${d1} in terms of years`]
} else if[date1.getDate[] === date2.getDate[]]{
console.log[`Both dates are equal`]
}
}
compareDates["9/10/1997", "9/10/2000"]
compareDates["11/11/2021", "11/1/2021"]
5, function compareDates[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1.getTime[] > date2.getTime[]]{
console.log[`${d1} is greater than ${d2} in terms of milliseconds`]
} else if[date1.getYear[] < date2.getYear[]]{
console.log[`${d2} is greater than ${d1} in terms of years`]
} else if[date1.getDate[] === date2.getDate[]]{
console.log[`Both dates are equal`]
}
}
compareDates["9/10/1997", "9/10/2000"]
compareDates["11/11/2021", "11/1/2021"]
6, function compareDates[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1.getTime[] > date2.getTime[]]{
console.log[`${d1} is greater than ${d2} in terms of milliseconds`]
} else if[date1.getYear[] < date2.getYear[]]{
console.log[`${d2} is greater than ${d1} in terms of years`]
} else if[date1.getDate[] === date2.getDate[]]{
console.log[`Both dates are equal`]
}
}
compareDates["9/10/1997", "9/10/2000"]
compareDates["11/11/2021", "11/1/2021"]
7, v.v. các phương thức trả về các mã định danh tạm thời đã cho, được phân vùng cụ thể cho UTCGhi chú. Với phương pháp này, bạn có thể sử dụng toán tử bình đẳng
Hãy xem một ví dụ
function compareDates[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1.getTime[] > date2.getTime[]]{
console.log[`${d1} is greater than ${d2} in terms of milliseconds`]
} else if[date1.getYear[] < date2.getYear[]]{
console.log[`${d2} is greater than ${d1} in terms of years`]
} else if[date1.getDate[] === date2.getDate[]]{
console.log[`Both dates are equal`]
}
}
compareDates["9/10/1997", "9/10/2000"]
compareDates["11/11/2021", "11/1/2021"]
Kết quả này trong
6/11/2020 is greater than 7/8/2019
Both dates are equal
8Mặc dù vậy, vì chúng ta đang làm việc với các khối
function compareDates[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1.getTime[] > date2.getTime[]]{
console.log[`${d1} is greater than ${d2} in terms of milliseconds`]
} else if[date1.getYear[] < date2.getYear[]]{
console.log[`${d2} is greater than ${d1} in terms of years`]
} else if[date1.getDate[] === date2.getDate[]]{
console.log[`Both dates are equal`]
}
}
compareDates["9/10/1997", "9/10/2000"]
compareDates["11/11/2021", "11/1/2021"]
8 và function compareDates[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1.getTime[] > date2.getTime[]]{
console.log[`${d1} is greater than ${d2} in terms of milliseconds`]
} else if[date1.getYear[] < date2.getYear[]]{
console.log[`${d2} is greater than ${d1} in terms of years`]
} else if[date1.getDate[] === date2.getDate[]]{
console.log[`Both dates are equal`]
}
}
compareDates["9/10/1997", "9/10/2000"]
compareDates["11/11/2021", "11/1/2021"]
9, nên một số câu lệnh không bao giờ được thực thi. Ví dụ: 6/11/2020 is greater than 7/8/2019
Both dates are equal
80 và 6/11/2020 is greater than 7/8/2019
Both dates are equal
81 có cùng ngày, nhưng 6/11/2020 is greater than 7/8/2019
Both dates are equal
82 không cùng nămHãy xem hướng dẫn thực hành, thực tế của chúng tôi để học Git, với các phương pháp hay nhất, tiêu chuẩn được ngành chấp nhận và bao gồm bảng gian lận. Dừng các lệnh Git trên Google và thực sự tìm hiểu nó
Ví dụ, mã này
function dateCompare[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1 > date2]{
console.log[`${d1} is greater than ${d2}`]
} else if[date1 < date2]{
console.log[`${d2} is greater than ${d1}`]
} else{
console.log[`Both dates are equal`]
}
}
dateCompare["6/11/2020", "7/8/2019"]
dateCompare["01/01/2021", "01/01/2021"]
4Sẽ dẫn đến
function dateCompare[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1 > date2]{
console.log[`${d1} is greater than ${d2}`]
} else if[date1 < date2]{
console.log[`${d2} is greater than ${d1}`]
} else{
console.log[`Both dates are equal`]
}
}
dateCompare["6/11/2020", "7/8/2019"]
dateCompare["01/01/2021", "01/01/2021"]
5Vì chúng ta chỉ đang so sánh ngày, không tính đến năm
Phần kết luận
Trong bài viết này, chúng tôi đã giới thiệu sơ lược về cách JavaScript xử lý ngày tháng bằng cách sử dụng đối tượng
function dateCompare[d1, d2]{
const date1 = new Date[d1];
const date2 = new Date[d2];
if[date1 > date2]{
console.log[`${d1} is greater than ${d2}`]
} else if[date1 < date2]{
console.log[`${d2} is greater than ${d1}`]
} else{
console.log[`Both dates are equal`]
}
}
dateCompare["6/11/2020", "7/8/2019"]
dateCompare["01/01/2021", "01/01/2021"]
6. Sau đó, chúng tôi đã giới thiệu cách so sánh ngày tháng trong JavaScript, lưu ý một số phương pháp hữu ích