'Why isBefore method doesn't work as expected in dayjs?
I am trying to create a calendar with dayjs but doesn't work though the same code is working in momentjs.
My code is:
import dayjs from 'dayjs';
const calendar = [];
const today = dayjs();
const startDay = today.clone().startOf('month').startOf('week');
const endDay = today.clone().endOf('month').endOf('week');
let day = startDay.clone().subtract(1, 'day');
while (day.isBefore(endDay, 'day')) {
calendar.push(
Array(7)
.fill(0)
.map(() => day.add(1, 'day').clone() )
)
};
After running the code, it behaves like continuous loop and shows this error:
<--- Last few GCs --->
[9148:000002DE6245A350] 188279 ms: Scavenge (reduce) 1987.6 (1992.3) -> 1986.8 (1993.3) MB, 5.3 / 0.0 ms (average mu = 0.253, current mu = 0.221) allocation failure
[9148:000002DE6245A350] 188284 ms: Scavenge (reduce) 1987.7 (1992.3) -> 1987.0 (1993.5) MB, 3.3 / 0.0 ms (average mu = 0.253, current mu = 0.221) allocation failure
[9148:000002DE6245A350] 188323 ms: Scavenge (reduce) 1987.8 (1992.5) -> 1987.0 (1993.8) MB, 5.3 / 0.0 ms (average mu = 0.253, current mu = 0.221) allocation failure
<--- JS stacktrace --->
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
1: 00007FF742FB481F napi_wrap+110783
2: 00007FF742F57F26 v8::base::CPU::has_sse+61862
3: 00007FF742F58E26 node::OnFatalError+294
4: 00007FF7438323BE v8::Isolate::ReportExternalAllocationLimitReached+94
5: 00007FF74381718D v8::SharedArrayBuffer::Externalize+781
6: 00007FF7436C02CC v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1516
7: 00007FF7436CB6EA v8::internal::Heap::ProtectUnprotectedMemoryChunks+1258
8: 00007FF7436C8829 v8::internal::Heap::PageFlagsAreConsistent+2457
9: 00007FF7436BD3D1 v8::internal::Heap::CollectGarbage+2049
10: 00007FF7436BB5D5 v8::internal::Heap::AllocateExternalBackingStore+1349
11: 00007FF7436DBA3B v8::internal::Factory::NewFillerObject+203
12: 00007FF74340A0B1 v8::internal::interpreter::JumpTableTargetOffsets::iterator::operator=+1409
13: 00007FF7438BB27D v8::internal::SetupIsolateDelegate::SetupHeap+465325
14: 000001673571CCAD
If I use the same code in momentjs, There is no issue and work well
What's the problem with dayjs that it doesn't working???
Solution 1:[1]
Had you try to log the day value inside the while?
put an day = day.add(1, 'day'); at the end of cicle
const calendar = [];
const today = dayjs();
const startDay = today.clone().startOf('month').startOf('week');
const endDay = today.clone().endOf('month').endOf('week');
let day = startDay.clone().subtract(1, 'day');
while (day.isBefore(endDay, 'day')) {
console.log(day)
calendar.push(
Array(7)
.fill(0)
.map(() => day.add(1, 'day').clone() )
)
day = day.add(1, 'day');
};
console.log(calendar)
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.10.6/dayjs.min.js"></script>
Solution 2:[2]
Day.js is immutable,it means you always get a clone of day after day.add(1, 'day') so the day value is never updated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AlTheLazyMonkey |
| Solution 2 | Vadim Kazykhanov |
