@loykin/datetime-range 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +13 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +13 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -953,17 +953,20 @@ function getSegmentValue(date, key) {
|
|
|
953
953
|
return date.getSeconds();
|
|
954
954
|
}
|
|
955
955
|
}
|
|
956
|
+
function daysInMonth(year, monthIndex) {
|
|
957
|
+
return new Date(year, monthIndex + 1, 0).getDate();
|
|
958
|
+
}
|
|
956
959
|
function applySegment(date, key, value) {
|
|
957
960
|
const d = new Date(date);
|
|
958
961
|
switch (key) {
|
|
959
962
|
case "year":
|
|
960
|
-
d.setFullYear(value);
|
|
963
|
+
d.setFullYear(value, d.getMonth(), Math.min(d.getDate(), daysInMonth(value, d.getMonth())));
|
|
961
964
|
break;
|
|
962
965
|
case "month":
|
|
963
|
-
d.setMonth(value - 1);
|
|
966
|
+
d.setMonth(value - 1, Math.min(d.getDate(), daysInMonth(d.getFullYear(), value - 1)));
|
|
964
967
|
break;
|
|
965
968
|
case "day":
|
|
966
|
-
d.setDate(value);
|
|
969
|
+
d.setDate(Math.min(value, daysInMonth(d.getFullYear(), d.getMonth())));
|
|
967
970
|
break;
|
|
968
971
|
case "hour":
|
|
969
972
|
d.setHours(value);
|
|
@@ -1199,8 +1202,9 @@ function toDisplayString(value, options) {
|
|
|
1199
1202
|
}
|
|
1200
1203
|
function toUrlString(value) {
|
|
1201
1204
|
if (value.type === "absolute") {
|
|
1202
|
-
|
|
1203
|
-
|
|
1205
|
+
if (!value.absoluteValue) return "now";
|
|
1206
|
+
const time = new Date(value.absoluteValue).getTime();
|
|
1207
|
+
return Number.isNaN(time) ? "now" : time.toString();
|
|
1204
1208
|
}
|
|
1205
1209
|
if (value.relativeNow) return "now";
|
|
1206
1210
|
if (typeof value.relativeValue === "string" && !value.relativeFormat) {
|
|
@@ -1242,8 +1246,10 @@ function fromUrlString(urlString) {
|
|
|
1242
1246
|
}
|
|
1243
1247
|
return { type: "relative", relativeNow: true };
|
|
1244
1248
|
}
|
|
1245
|
-
|
|
1246
|
-
|
|
1249
|
+
if (/^-?\d+$/.test(urlString)) {
|
|
1250
|
+
const date = new Date(Number(urlString));
|
|
1251
|
+
if (!Number.isNaN(date.getTime())) return { type: "absolute", absoluteValue: date };
|
|
1252
|
+
}
|
|
1247
1253
|
return { type: "relative", relativeNow: true };
|
|
1248
1254
|
}
|
|
1249
1255
|
function relativeNow() {
|