เพราะว่านิยามของ"อายุอยู่ในเดือน"คือ...เรื่องมากเกี่ยวมทางที่ง่ายที่สุดคือการใช้ดาวดวงเล็กน้อยรวมอัตโนมัติอย่างที่คุณจะสะทกสะทานเลยละสิมันอยู่ในความคิดของหล่อนและไม่เกี่ยวข้องกับที่พว Date
ห้องเรียน
สำหรับค[ค]นมนุษย์ตีความแบบนั้นของ"อายุอยู่ในเดือน"ที่ออกกฏก็คือ
สะทกสะทานเลยละสิความแตกต่างระหว่างที่สองวันที่ในเดือน
เหมือนว่าวันที่ของคนเดือนเป็นวันที่ 1 ของเดือนสำหรับทั้งสองวัน
ลบ 1 จากกลุ่มสุดท้ายเดือน
แล้วถ้าวันที่ของคนเดือนของคน เมื่อ วันในช่วงเวลาอยู่
หรือหลังจากวันที่ของคนเดือนของ ก่อน วันของช่วงเวลาที่[อาจจะแค่ส่วนนึงของ]สุดท้ายเดือนเป็นสมบูรณ์:เพิ่ม 1 เพื่อเรียกคืนนับได้
คนบินอยู่คนขี้ผึ้งมาทาเสียนะนี่ตั้งแต่เดือนมีต่างกันตัวเลขของวันก่อนการเกี่ยวข้องกับคดีก็ต่อเมื่อ 2 เดือนแตกต่างกันในของจำนวนของวันแล้ว
ถ้าอย่างไรก็ตามที่สิ้นสุดที่เดือนมันสั้นกว่ากเริ่มที่เดือน,คุณสามารถเข้าไปในสถานการณ์ที่ขอบเขตอาการไม่สามารถจะพบกัน(e.g. เริ่มเดทกันเป็นเดือนกุมภาพันธ์ 28th และวันสิ้นสุดคือเดือนมีนาควันที่ 31@item:inlistbox last day of the month. เพื่อแก้ไขที่คุณต้องดู"จุดจบของเดือน"จะเป็นหน้าต่าง ranging จากวันนี้เป็นวันสุดท้ายของเริ่มที่เดือนผ่านวันสุดท้ายของสิ้นสุดที่เดือนคิดถึงทุกฝ่า.
นั่นคือทางนี้เป็นรหัสมอส ฉันกำลังใช้โครงสร้างของชอบต่อไปนี้จะเป็นตัวแทนของวันที่:
{
year: 2021 , // 4-digit year
month: 11 , // month of year (1-12 mapping to January-December)
day: 23 // day of month (1-[28-31] depending on year/month
}
Ensuring นข้อมูลในนั้น struct เป็นตัวแทนที่ใช้งานได้วันที่เหลือเป็นแบบฝึกหัดสำหรับเครื่องมืออ่าน.
รหัสไม่ใช่อย่างนั้นซับซ้อน:
/**
*
* @param {object} bgn - start date of period
* @param {number} bgn.year - 4-digit year
* @param {number} bgn.month - month of year [1-12]
* @param {number} bgn.day - day of month [1-31]
*
* @param {object} end - end date of period
* @param {number} end.year - 4-digit year
* @param {number} end.month - month of year [1-12]
* @param {number} end.day - day of month [1-31]
*
*/
function diffInMonths( bgn , end ) {
const between = ( x , min , max ) => x >= min && x <= max;
// We'll need to add back the final month based on the following:
// - end.day >= bgn.day -- we've passed the month boundary, or
// - end.day is within the end-of-month window
// (when the end month is shorter than the start month)
const needAdjustment = end.day >= bgn.day
|| between( end.day, daysInMonth(bgn), daysInMonth(end) );
const finalMonthAdjustment = needsAdjustment ? 1 : 0;
const deltaM = 12 * ( end.year - bgn.year )
+ ( end.month - bgn.month )
- 1 // remove the final month from the equation
+ finalMonthAdjustment // add in the precomputed final month adjustment
;
return deltaM;
}
/**
*
* @param {object} dt - date
* @param {number} dt.year - 4-digit year
* @param {number} dt.month - month of year [1-12]
* @param {number} dt.day - day of month [1-31]
*
*/
function daysInMonth(dt) {
const leapYear = ( dt.year % 4 === 0 && dt.year % 100 !== 0 ) || dt.year % 400 === 0;
const monthDays = leapYear ? daysPerMonthLeap : daysPerMonth;
const days = monthDays[dt.month];
return days;
}
// jan feb mar apr may jun jul aug sep oct nov dec
// ---------- --- --- --- --- --- --- --- --- --- --- --- ---
const daysPerMonth = [ undefined, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, ];
const daysPerMonthLeap = [ undefined, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, ];
new Date(userDate.split('/').reverse())
เกือบมันใจกลับมาเป็นไม่ถูกต้องออกเดท