Yêu cầu. Cho trước ngày bắt đầu và ngày kết thúc, chúng ta cần tìm xem giữa chúng có những ngày nào trong tuần. e. CN, T2, T3, T4, T5, T6, T7 (bằng số. 0, 1, 2, 3, 4, 5, 6). Chúng tôi cũng có thể muốn đếm chúng hoặc chúng tôi chỉ quan tâm nếu có cuối tuần
Điều này thường được yêu cầu trong các biểu mẫu mà chúng tôi muốn tạo thứ gì đó định kỳ hoặc hàng loạt và chúng tôi cần biết liệu một ngày trong tuần cụ thể có nằm giữa các ngày do người dùng chọn hay không
Dưới đây là mã có giải thích, sử dụng cả ngày và thời điểm JavaScript js
Nhận số ngày trong tuần giữa hai ngày
1. Với ngày JavaScript
const getDaysOfWeekBetweenDates = (sDate = "2021-07-18", eDate = "2021-07-20") => { const startDate = new Date(sDate) const endDate = new Date(eDate); endDate.setDate(endDate.getDate() + 1); const daysOfWeek = []; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek.push(startDate.getDay()); startDate.setDate(startDate.getDate() + 1); i++; } return daysOfWeek; }; getDaysOfWeekBetweenDates("2021-07-01", "2021-07-04"); // => [ 4, 5, 6, 0 ] getDaysOfWeekBetweenDates("2021-07-15", "2021-07-29"); // => [ 4, 5, 6, 0, 1, 2, 3 ] getDaysOfWeekBetweenDates("1999-01-01", "1999-01-06"); // => [ 5, 6, 0, 1, 2, 3 ] getDaysOfWeekBetweenDates("2011-06-11", "2007-06-18") // => []
2. Với khoảnh khắc JS
const moment = require("moment"); const getDaysOfWeekBetweenDates = (sDate = "2021-07-18", eDate = "2021-07-20") => { const startDate = moment(sDate) const endDate = moment(eDate); endDate.add(1, "day"); const daysOfWeek = []; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek.push(startDate.day()); startDate.add(1, "day"); i++; } return daysOfWeek; }; getDaysOfWeekBetweenDates("2021-07-02", "2021-07-05"); // => [ 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("2010-07-20", "2010-07-28"); // => [ 2, 3, 4, 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("1999-06-12", "1999-06-17"); // => [ 6, 0, 1, 2, 3, 4 ] getDaysOfWeekBetweenDates("2015-06-11", "2000-06-16") // => []
Mã giải thích
Trong cả hai phương pháp trên, các bước vẫn giống nhau
- Tạo một đối tượng Ngày hoặc thời điểm mới cho ngày bắt đầu và ngày kết thúc
- Thêm 1 ngày vào ngày kết thúc vì chúng tôi muốn đưa ngày kết thúc vào tính toán của mình
- khởi tạo mảng const moment = require("moment"); const getDaysOfWeekBetweenDates = (sDate = "2021-07-18", eDate = "2021-07-20") => { const startDate = moment(sDate) const endDate = moment(eDate); endDate.add(1, "day"); const daysOfWeek = []; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek.push(startDate.day()); startDate.add(1, "day"); i++; } return daysOfWeek; }; getDaysOfWeekBetweenDates("2021-07-02", "2021-07-05"); // => [ 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("2010-07-20", "2010-07-28"); // => [ 2, 3, 4, 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("1999-06-12", "1999-06-17"); // => [ 6, 0, 1, 2, 3, 4 ] getDaysOfWeekBetweenDates("2015-06-11", "2000-06-16") // => [] 6 trống và bộ đếm const moment = require("moment"); const getDaysOfWeekBetweenDates = (sDate = "2021-07-18", eDate = "2021-07-20") => { const startDate = moment(sDate) const endDate = moment(eDate); endDate.add(1, "day"); const daysOfWeek = []; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek.push(startDate.day()); startDate.add(1, "day"); i++; } return daysOfWeek; }; getDaysOfWeekBetweenDates("2021-07-02", "2021-07-05"); // => [ 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("2010-07-20", "2010-07-28"); // => [ 2, 3, 4, 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("1999-06-12", "1999-06-17"); // => [ 6, 0, 1, 2, 3, 4 ] getDaysOfWeekBetweenDates("2015-06-11", "2000-06-16") // => [] 7
- thêm vòng lặp const moment = require("moment"); const getDaysOfWeekBetweenDates = (sDate = "2021-07-18", eDate = "2021-07-20") => { const startDate = moment(sDate) const endDate = moment(eDate); endDate.add(1, "day"); const daysOfWeek = []; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek.push(startDate.day()); startDate.add(1, "day"); i++; } return daysOfWeek; }; getDaysOfWeekBetweenDates("2021-07-02", "2021-07-05"); // => [ 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("2010-07-20", "2010-07-28"); // => [ 2, 3, 4, 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("1999-06-12", "1999-06-17"); // => [ 6, 0, 1, 2, 3, 4 ] getDaysOfWeekBetweenDates("2015-06-11", "2000-06-16") // => [] 8 để kiểm tra bộ đếm nhỏ hơn 7 (vì số ngày tối đa trong tuần bằng 7) và startDate nhỏ hơn endDate
- Bên trong vòng lặp const moment = require("moment"); const getDaysOfWeekBetweenDates = (sDate = "2021-07-18", eDate = "2021-07-20") => { const startDate = moment(sDate) const endDate = moment(eDate); endDate.add(1, "day"); const daysOfWeek = []; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek.push(startDate.day()); startDate.add(1, "day"); i++; } return daysOfWeek; }; getDaysOfWeekBetweenDates("2021-07-02", "2021-07-05"); // => [ 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("2010-07-20", "2010-07-28"); // => [ 2, 3, 4, 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("1999-06-12", "1999-06-17"); // => [ 6, 0, 1, 2, 3, 4 ] getDaysOfWeekBetweenDates("2015-06-11", "2000-06-16") // => [] 8, chúng ta đẩy ngày vào mảng const moment = require("moment"); const getDaysOfWeekBetweenDates = (sDate = "2021-07-18", eDate = "2021-07-20") => { const startDate = moment(sDate) const endDate = moment(eDate); endDate.add(1, "day"); const daysOfWeek = []; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek.push(startDate.day()); startDate.add(1, "day"); i++; } return daysOfWeek; }; getDaysOfWeekBetweenDates("2021-07-02", "2021-07-05"); // => [ 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("2010-07-20", "2010-07-28"); // => [ 2, 3, 4, 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("1999-06-12", "1999-06-17"); // => [ 6, 0, 1, 2, 3, 4 ] getDaysOfWeekBetweenDates("2015-06-11", "2000-06-16") // => [] 6, thêm 1 ngày vào startDate và tăng bộ đếm const moment = require("moment"); const getDaysOfWeekBetweenDates = (sDate = "2021-07-18", eDate = "2021-07-20") => { const startDate = moment(sDate) const endDate = moment(eDate); endDate.add(1, "day"); const daysOfWeek = []; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek.push(startDate.day()); startDate.add(1, "day"); i++; } return daysOfWeek; }; getDaysOfWeekBetweenDates("2021-07-02", "2021-07-05"); // => [ 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("2010-07-20", "2010-07-28"); // => [ 2, 3, 4, 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("1999-06-12", "1999-06-17"); // => [ 6, 0, 1, 2, 3, 4 ] getDaysOfWeekBetweenDates("2015-06-11", "2000-06-16") // => [] 7
- cuối cùng, trả về mảng const moment = require("moment"); const getDaysOfWeekBetweenDates = (sDate = "2021-07-18", eDate = "2021-07-20") => { const startDate = moment(sDate) const endDate = moment(eDate); endDate.add(1, "day"); const daysOfWeek = []; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek.push(startDate.day()); startDate.add(1, "day"); i++; } return daysOfWeek; }; getDaysOfWeekBetweenDates("2021-07-02", "2021-07-05"); // => [ 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("2010-07-20", "2010-07-28"); // => [ 2, 3, 4, 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("1999-06-12", "1999-06-17"); // => [ 6, 0, 1, 2, 3, 4 ] getDaysOfWeekBetweenDates("2015-06-11", "2000-06-16") // => [] 6
Trả về đối tượng thay vì mảng
Lưu ý rằng chúng ta cũng có thể sử dụng hàm băm đối tượng để lưu trữ từ 0 đến 6 và gán chúng là true nếu ngày được tìm thấy. Từ ví dụ về thời điểm trên, chúng tôi sẽ thay thế phần sau
const daysOfWeek = {}; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek[startDate.day()] = true; startDate.add(1, "day"); i++; } return daysOfWeek;
Và kết quả cho các cuộc gọi tương tự sẽ trở thành
getDaysOfWeekBetweenDates("2021-07-02", "2021-07-05"); // => { '0': true, '1': true, '5': true, '6': true } getDaysOfWeekBetweenDates("2010-07-20", "2010-07-28"); /* => { '0': true, '1': true, '2': true, '3': true, '4': true, '5': true, '6': true } */ getDaysOfWeekBetweenDates("1999-06-12", "1999-06-17") // => { '0': true, '1': true, '2': true, '3': true, '4': true, '6': true } getDaysOfWeekBetweenDates("2015-06-11", "2000-06-16") // => {}
Trả lại tên ngày thay vì số
Để làm được điều đó, hãy sửa đổi đoạn mã trên một chút và sử dụng mảng tên ngày const daysOfWeek = {}; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek[startDate.day()] = true; startDate.add(1, "day"); i++; } return daysOfWeek; 3
const dayNames = [ "sun", "mon", "tue", "wed", "thu", "fri", "sat" ]; // add this at the top daysOfWeek.push(dayNames[startDate.getDay()]); // modify this inside while loop // OR in case of returning the hash daysOfWeek[dayNames[startDate.day()]] = true;
Kết quả bây giờ sẽ giống như
// array // => [ 'fri', 'sat', 'sun', 'mon', 'tue', 'wed' ] // hash // => { // sat: true, // sun: true, // mon: true, // tue: true, // wed: true, // thu: true // }
Những câu hỏi nào có thể được trả lời từ dữ liệu này?
Chúng ta có thể sử dụng mảng trả về hoặc hàm băm để trả lời các câu hỏi về các ngày trong tuần, chẳng hạn như
Thứ năm có tồn tại không?
// array daysOfWeek.includes(4) // => true or false // hash daysOfWeek[4] // => true or undefined
Có một ngày cuối tuần tồn tại?
// array daysOfWeek.includes(0) || daysOfWeek.includes(6) // hash daysOfWeek[0] || daysOfWeek[6]
Cả Thứ Bảy và Chủ Nhật đều tồn tại (Cuối tuần hoàn toàn tồn tại?)
// array daysOfWeek.includes(0) && daysOfWeek.includes(6) // hash daysOfWeek[0] && daysOfWeek[6]
Không có cuối tuần tồn tại?
// array !daysOfWeek.includes(0) && !daysOfWeek.includes(6) // hash !daysOfWeek[0] && !daysOfWeek[6]
Đếm ngày trong tuần giữa hai ngày
Với một chút sửa đổi đối với mã ban đầu của chúng tôi, chúng tôi có thể giữ và tăng số lượng tất cả các ngày trong tuần giữa hai ngày
1. Với ngày JavaScript
const moment = require("moment"); const getDaysOfWeekBetweenDates = (sDate = "2021-07-18", eDate = "2021-07-20") => { const startDate = moment(sDate) const endDate = moment(eDate); endDate.add(1, "day"); const daysOfWeek = []; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek.push(startDate.day()); startDate.add(1, "day"); i++; } return daysOfWeek; }; getDaysOfWeekBetweenDates("2021-07-02", "2021-07-05"); // => [ 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("2010-07-20", "2010-07-28"); // => [ 2, 3, 4, 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("1999-06-12", "1999-06-17"); // => [ 6, 0, 1, 2, 3, 4 ] getDaysOfWeekBetweenDates("2015-06-11", "2000-06-16") // => [] 0
2. Với khoảnh khắc JS
const moment = require("moment"); const getDaysOfWeekBetweenDates = (sDate = "2021-07-18", eDate = "2021-07-20") => { const startDate = moment(sDate) const endDate = moment(eDate); endDate.add(1, "day"); const daysOfWeek = []; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek.push(startDate.day()); startDate.add(1, "day"); i++; } return daysOfWeek; }; getDaysOfWeekBetweenDates("2021-07-02", "2021-07-05"); // => [ 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("2010-07-20", "2010-07-28"); // => [ 2, 3, 4, 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("1999-06-12", "1999-06-17"); // => [ 6, 0, 1, 2, 3, 4 ] getDaysOfWeekBetweenDates("2015-06-11", "2000-06-16") // => [] 1
Kết quả
Gọi const daysOfWeek = {}; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek[startDate.day()] = true; startDate.add(1, "day"); i++; } return daysOfWeek; 4 trả về cho chúng tôi một hàm băm đối tượng với các khóa từ 0 đến 6 và số lượng mỗi ngày đối với chúng, trong ngày bắt đầu và ngày kết thúc đã cho
const moment = require("moment"); const getDaysOfWeekBetweenDates = (sDate = "2021-07-18", eDate = "2021-07-20") => { const startDate = moment(sDate) const endDate = moment(eDate); endDate.add(1, "day"); const daysOfWeek = []; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek.push(startDate.day()); startDate.add(1, "day"); i++; } return daysOfWeek; }; getDaysOfWeekBetweenDates("2021-07-02", "2021-07-05"); // => [ 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("2010-07-20", "2010-07-28"); // => [ 2, 3, 4, 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("1999-06-12", "1999-06-17"); // => [ 6, 0, 1, 2, 3, 4 ] getDaysOfWeekBetweenDates("2015-06-11", "2000-06-16") // => [] 2
Từ đối tượng kết quả, chúng ta có thể cho biết số lượng của bất kỳ ngày nào trong tuần hoặc kết hợp chúng một cách riêng lẻ. Ví dụ
Đếm ngày chủ nhật kể từ khi bắt đầu lịch
const moment = require("moment"); const getDaysOfWeekBetweenDates = (sDate = "2021-07-18", eDate = "2021-07-20") => { const startDate = moment(sDate) const endDate = moment(eDate); endDate.add(1, "day"); const daysOfWeek = []; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek.push(startDate.day()); startDate.add(1, "day"); i++; } return daysOfWeek; }; getDaysOfWeekBetweenDates("2021-07-02", "2021-07-05"); // => [ 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("2010-07-20", "2010-07-28"); // => [ 2, 3, 4, 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("1999-06-12", "1999-06-17"); // => [ 6, 0, 1, 2, 3, 4 ] getDaysOfWeekBetweenDates("2015-06-11", "2000-06-16") // => [] 3
Đếm các ngày cuối tuần giữa Thế chiến 1 và Thế chiến 2
const moment = require("moment"); const getDaysOfWeekBetweenDates = (sDate = "2021-07-18", eDate = "2021-07-20") => { const startDate = moment(sDate) const endDate = moment(eDate); endDate.add(1, "day"); const daysOfWeek = []; let i = 0; while (i < 7 && startDate < endDate) { daysOfWeek.push(startDate.day()); startDate.add(1, "day"); i++; } return daysOfWeek; }; getDaysOfWeekBetweenDates("2021-07-02", "2021-07-05"); // => [ 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("2010-07-20", "2010-07-28"); // => [ 2, 3, 4, 5, 6, 0, 1 ] getDaysOfWeekBetweenDates("1999-06-12", "1999-06-17"); // => [ 6, 0, 1, 2, 3, 4 ] getDaysOfWeekBetweenDates("2015-06-11", "2000-06-16") // => [] 4
Mặc dù có những cách khác để tìm ra điều đó, nhưng chúng ta cũng có thể tính tổng tất cả các ngày trong tuần và nhận tổng số ngày giữa hai ngày. Ví dụ