为什么 isBefore 方法在 dayjs 中不能按预期工作?
Why isBefore method doesn't work as expected in dayjs?
我正在尝试使用 dayjs 创建日历,但无法正常工作,尽管相同的代码在 momentjs 中工作。
我的代码是:
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() )
)
};
在 运行 代码之后,它的行为类似于连续循环并显示此错误:
<--- 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
如果我在 momentjs 中使用相同的代码,没有问题并且运行良好
dayjs 有什么问题不能用???
您是否尝试过在 while 内记录日期值?
在循环的末尾放一个day = day.add(1, 'day');
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>
Day.js 是不可变的,这意味着您总是在 day.add(1, 'day')
之后获得 day
的克隆,因此日期值永远不会更新。
我正在尝试使用 dayjs 创建日历,但无法正常工作,尽管相同的代码在 momentjs 中工作。
我的代码是:
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() )
)
};
在 运行 代码之后,它的行为类似于连续循环并显示此错误:
<--- 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
如果我在 momentjs 中使用相同的代码,没有问题并且运行良好
dayjs 有什么问题不能用???
您是否尝试过在 while 内记录日期值?
在循环的末尾放一个day = day.add(1, 'day');
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>
Day.js 是不可变的,这意味着您总是在 day.add(1, 'day')
之后获得 day
的克隆,因此日期值永远不会更新。