]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Calculate daysInMonth without Date()
authorKunal Marwaha <marwahaha@berkeley.edu>
Wed, 29 Mar 2017 06:35:52 +0000 (02:35 -0400)
committerIskren Chernev <iskren.chernev@gmail.com>
Sun, 6 Aug 2017 23:29:10 +0000 (02:29 +0300)
src/lib/units/month.js
src/lib/units/year.js
src/lib/utils/mod.js [new file with mode: 0644]

index 28b4a1b984686c8ba8f930d409f604c02008aed8..6f682f3f8e9bee71927a6dd8a5da4a4294334535 100644 (file)
@@ -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
index a10e5b4beb3588238208508569b7c4f953aede7e..8f3f94cdab1cea5bcdd4696c0933d13893de7da4 100644 (file)
@@ -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 (file)
index 0000000..8046cda
--- /dev/null
@@ -0,0 +1,3 @@
+export default function mod(n, x) {
+    return ((n % x) + x) % x;
+}