0

我正在尝试使用 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 不工作有什么问题???

4

1 回答 1

1

您是否尝试在一段时间内记录日值?

day = day.add(1, 'day');在 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>

于 2021-07-23T15:26:44.067 回答