Sử dụng lớp Date của JavaScript, đối tượng new Date() trả về ngày hiện tại. Chúng ta cần tìm ngày của hai ngày tiếp theo.
Đây là vấn đề đơn giản, chỉ cần vài dòng mã. Trước tiên lấy ngày hôm nay:
// lấy ngày hôm nay const today = new Date();
Ví dụ
Mã thực hiện:
// lấy ngày hôm nay const today = new Date(); // khởi tạo tomorrow với ngày hôm nay const tomorrow = new Date(today); // tăng một ngày cho tomorrow tomorrow.setDate(tomorrow.getDate() + 1); const dayAfterTomorrow = new Date(today); dayAfterTomorrow.setDate(dayAfterTomorrow.getDate() + 2); console.log(today); console.log(tomorrow); console.log(dayAfterTomorrow);
Kết quả
Kết quả in ra console:
2020-08-13T17:13:26.401Z 2020-08-14T17:13:26.401Z 2020-08-15T17:13:26.401Z