Tôi đã xem xét các DateJS và loại bỏ mã cần thiết để thêm hàng tháng vào các trường hợp cạnh xử lý ngày [năm nhảy, tháng ngắn hơn, v.v.]:
Date.isLeapYear = function [year] {
return [[[year % 4 === 0] && [year % 100 !== 0]] || [year % 400 === 0]];
};
Date.getDaysInMonth = function [year, month] {
return [31, [Date.isLeapYear[year] ? 29 : 28], 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
};
Date.prototype.isLeapYear = function [] {
return Date.isLeapYear[this.getFullYear[]];
};
Date.prototype.getDaysInMonth = function [] {
return Date.getDaysInMonth[this.getFullYear[], this.getMonth[]];
};
Date.prototype.addMonths = function [value] {
var n = this.getDate[];
this.setDate[1];
this.setMonth[this.getMonth[] + value];
this.setDate[Math.min[n, this.getDaysInMonth[]]];
return this;
};
Điều này sẽ thêm hàm "AddMonths []" vào bất kỳ đối tượng Ngày JavaScript nào nên xử lý các trường hợp cạnh. Cảm ơn Coolite Inc!
Sử dụng:
var myDate = new Date["01/31/2012"];
var result1 = myDate.addMonths[1];
var myDate2 = new Date["01/31/2011"];
var result2 = myDate2.addMonths[1];
->> newdate.addmonths -> mydate.addmonths
Kết quả1 = "Ngày 29 tháng 2 năm 2012"
Kết quả2 = "Ngày 28 tháng 2 năm 2011"
Thêm tháng vào ngày trong JavaScript #
Để thêm tháng vào một ngày:
- Sử dụng phương thức
getMonth[]
để nhận giá trị dựa trên không trong tháng của ngày đã cho. - Sử dụng phương thức
setMonth[]
để đặt tháng cho ngày. - Phương thức
setMonth
có số nguyên dựa trên không trong tháng và đặt giá trị cho ngày.
Copied!
function addMonths[numOfMonths, date = new Date[]] { date.setMonth[date.getMonth[] + numOfMonths]; return date; } // 👇️ Add 2 months to current Date const result = addMonths[2]; // 👇️ Add months to another date const date = new Date['2022-02-24']; console.log[addMonths[3, date]]; // 👉️ Tue May 24 2022
Chúng tôi đã tạo ra một chức năng có thể tái sử dụng mất số tháng và đối tượng Date
và thêm các tháng vào ngày.
Nếu không có đối tượng Date
được cung cấp cho chức năng, nó sẽ sử dụng ngày hiện tại.
Phương thức GetMonth [] trả về một số nguyên giữa ____10 [tháng 1] và
var myDate = new Date["01/31/2012"];
var result1 = myDate.addMonths[1];
var myDate2 = new Date["01/31/2011"];
var result2 = myDate2.addMonths[1];
1 [tháng 12], đại diện cho tháng của ngày nhất định.Lưu ý rằng giá trị là dựa trên 0, ví dụ: Tháng 1 = 0, tháng 2 = 1, tháng 3 = 2, v.v.
Phương thức setMonth [] có giá trị dựa trên không đại diện cho tháng trong năm [0 = tháng 1, 1 = tháng 2, v.v.] và đặt giá trị cho ngày.
Đối tượng JavaScript Date
tự động chăm sóc việc lăn trong năm nếu thêm X tháng vào một ngày đẩy chúng tôi vào năm tới.
Copied!
const date = new Date['2022-12-24']; date.setMonth[date.getMonth[] + 3]; console.log[date]; // 👉️ Fri Mar 24 2023 [year adjusted]
Chúng tôi đã thêm
var myDate = new Date["01/31/2012"];
var result1 = myDate.addMonths[1];
var myDate2 = new Date["01/31/2011"];
var result2 = myDate2.addMonths[1];
3 tháng cho đến ngày, điều này đã đẩy chúng tôi vào năm tới và đối tượng Date
tự động chăm sóc cập nhật năm.Lưu ý rằng phương thức setMonth
làm biến đổi đối tượng Date
mà nó đã được gọi. Nếu bạn không muốn thay đổi Date
tại chỗ, bạn có thể tạo một bản sao của nó trước khi gọi phương thức.
Copied!
function addMonths[numOfMonths, date = new Date[]] { const dateCopy = new Date[date.getTime[]]; dateCopy.setMonth[dateCopy.getMonth[] + numOfMonths]; return dateCopy; } const date = new Date['2022-02-24']; const result = addMonths[1, date]; console.log[result]; // 👉️ Thu Mar 24 2022 console.log[date]; // 👉️ Thu Feb 24 2022 [didn't change original]
Phương pháp GetTime trả về số mili giây trôi qua trong khoảng thời gian từ ngày 1 tháng 1 năm 1970 00:00:00 và ngày nhất định.
Chúng tôi đã sử dụng dấu thời gian để tạo một bản sao của đối tượng Date
, vì vậy chúng tôi không đột biến nó tại chỗ khi gọi phương thức setMonth
.
Sao chép ngày khá hữu ích khi bạn phải sử dụng đối tượng Date
gốc ở những nơi khác trong mã của bạn.
Nói chung, các đối số chức năng đột biến là một thực tế xấu, vì chuyển cùng một tham số cho cùng một hàm nhiều lần tạo ra các kết quả khác nhau.
Bạn có thể thấy phương thức setMonth
được sử dụng với các tham số
2. Các tham số mà phương thức thực hiện là:Copied!
function addMonths[numOfMonths, date = new Date[]] { date.setMonth[date.getMonth[] + numOfMonths]; return date; } // 👇️ Add 2 months to current Date const result = addMonths[2]; // 👇️ Add months to another date const date = new Date['2022-02-24']; console.log[addMonths[3, date]]; // 👉️ Tue May 24 2022
3 - Giá trị không dựa trên không [0 = tháng 1, 1 = tháng 2, v.v.] cho tháng trong năm.Copied!
function addMonths[numOfMonths, date = new Date[]] { date.setMonth[date.getMonth[] + numOfMonths]; return date; } // 👇️ Add 2 months to current Date const result = addMonths[2]; // 👇️ Add months to another date const date = new Date['2022-02-24']; console.log[addMonths[3, date]]; // 👉️ Tue May 24 2022
4 [Tùy chọn] - Một số nguyên từCopied!
function addMonths[numOfMonths, date = new Date[]] { date.setMonth[date.getMonth[] + numOfMonths]; return date; } // 👇️ Add 2 months to current Date const result = addMonths[2]; // 👇️ Add months to another date const date = new Date['2022-02-24']; console.log[addMonths[3, date]]; // 👉️ Tue May 24 2022
5 đếnCopied!
function addMonths[numOfMonths, date = new Date[]] { date.setMonth[date.getMonth[] + numOfMonths]; return date; } // 👇️ Add 2 months to current Date const result = addMonths[2]; // 👇️ Add months to another date const date = new Date['2022-02-24']; console.log[addMonths[3, date]]; // 👉️ Tue May 24 2022
6 đại diện cho ngày của tháng.Copied!
function addMonths[numOfMonths, date = new Date[]] { date.setMonth[date.getMonth[] + numOfMonths]; return date; } // 👇️ Add 2 months to current Date const result = addMonths[2]; // 👇️ Add months to another date const date = new Date['2022-02-24']; console.log[addMonths[3, date]]; // 👉️ Tue May 24 2022
Tham số
7 là tùy chọn và khi không được chỉ định, giá trị được trả về từ phương thứcCopied!
function addMonths[numOfMonths, date = new Date[]] { date.setMonth[date.getMonth[] + numOfMonths]; return date; } // 👇️ Add 2 months to current Date const result = addMonths[2]; // 👇️ Add months to another date const date = new Date['2022-02-24']; console.log[addMonths[3, date]]; // 👉️ Tue May 24 2022
8 được sử dụng.Copied!
function addMonths[numOfMonths, date = new Date[]] { date.setMonth[date.getMonth[] + numOfMonths]; return date; } // 👇️ Add 2 months to current Date const result = addMonths[2]; // 👇️ Add months to another date const date = new Date['2022-02-24']; console.log[addMonths[3, date]]; // 👉️ Tue May 24 2022