Kiểm tra xem ngày có cũ hơn 7 ngày không javascript

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ây

Thờ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"]
6

Chú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ê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"]

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ợp

Ghi 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ông

Mộ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
7

Phươ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ác

Ngoà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 UTC

Ghi 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
8

Mặ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ăm

Hã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"]
4

Sẽ 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"]
5

Vì 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

Làm cách nào để có được Ngày 7 ngày tuổi trong JavaScript?

Giải pháp một dòng js thuần túy. .
new Date[] - tạo đối tượng Date từ thời gian tính bằng mili giây
Ngày tháng. now[] - đưa ra thời gian tính bằng mili giây từ năm 1970 đến nay
7 [ngày] * 24 [giờ] * 60 [phút] * 60 [giây] * 1000 [mili giây ] = 604800000 [7 ngày tính bằng mili giây]

Làm cách nào để kiểm tra xem Ngày có phải là Ngày cũ trong JavaScript không?

Để kiểm tra xem một ngày có trong quá khứ không. Sử dụng hàm tạo Date[] để lấy ngày hiện tại . Tùy chọn đặt thời gian của ngày hiện tại thành nửa đêm. Kiểm tra xem ngày có nhỏ hơn ngày hiện tại không.

Làm cách nào để kiểm tra xem Ngày có lớn hơn 30 ngày không JavaScript?

getTime[] - ba mươiDaysInMs; . log['ngày cách đây hơn 30 ngày']; . log['ngày KHÔNG quá 30 ngày trước']; . log[isMoreThan30DaysAgo[new Date['2021-09-24']]];

Làm cách nào để kiểm tra xem một Ngày có trong vòng 30 ngày trong JavaScript không?

Làm cách nào để kiểm tra xem một ngày có trong vòng 30 ngày hay không bằng JavaScript? .
Trừ dấu thời gian của ngày hiện tại khỏi dấu thời gian của ngày
Truyền kết quả cho Math. hàm abs[]
Chuyển đổi kết quả thành ngày
Kiểm tra xem các ngày giữa các ngày có nhỏ hơn 30 không

Chủ Đề