From: Kunal Marwaha Date: Wed, 29 Mar 2017 06:35:52 +0000 (-0400) Subject: Calculate daysInMonth without Date() X-Git-Tag: 2.19.0~33^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=41d900cfd4e99b3025697e96ba4d3637dbcf632a;p=thirdparty%2Fmoment.git Calculate daysInMonth without Date() --- diff --git a/src/lib/units/month.js b/src/lib/units/month.js index 28b4a1b98..6f682f3f8 100644 --- a/src/lib/units/month.js +++ b/src/lib/units/month.js @@ -10,12 +10,21 @@ import { MONTH } from './constants'; import toInt from '../utils/to-int'; import isArray from '../utils/is-array'; import isNumber from '../utils/is-number'; +import mod from '../utils/mod'; import indexOf from '../utils/index-of'; import { createUTC } from '../create/utc'; import getParsingFlags from '../create/parsing-flags'; +import { isLeapYear } from '../units/year'; export function daysInMonth(year, month) { - return new Date(Date.UTC(year, month + 1, 0)).getUTCDate(); + if (isNaN(year) || isNaN(month)) { + return NaN; + } + var modMonth = mod(month, 12); + if (modMonth === month) { + return month === 1 ? (isLeapYear(year) ? 29 : 28) : (31 - month % 7 % 2); + } + return daysInMonth(year + (month - modMonth) / 12, modMonth); } // FORMATTING diff --git a/src/lib/units/year.js b/src/lib/units/year.js index a10e5b4be..8f3f94cda 100644 --- a/src/lib/units/year.js +++ b/src/lib/units/year.js @@ -56,7 +56,7 @@ export function daysInYear(year) { return isLeapYear(year) ? 366 : 365; } -function isLeapYear(year) { +export function isLeapYear(year) { return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; } diff --git a/src/lib/utils/mod.js b/src/lib/utils/mod.js new file mode 100644 index 000000000..8046cdac9 --- /dev/null +++ b/src/lib/utils/mod.js @@ -0,0 +1,3 @@ +export default function mod(n, x) { + return ((n % x) + x) % x; +}