簡體   English   中英

錯誤:date-fns 不接受字符串作為日期參數。 請使用`parseISO`來解析字符串

[英]Error: date-fns doesn't accept strings as date arguments. Please use `parseISO` to parse strings

我在我的 Ionic 應用程序上使用https://github.com/mattlewis92/angular-calendar 當我從組件文件加載一系列事件時,一切都很好。 這就是我的做法:

.ts:
bookingsToLoad: BookingEvent[] = [
    {
      id: 1, 
      title: "First booking",
      start: new Date(2021, 6, 7, 12, 30, 0, 0), 
      end: new Date(2021, 6, 7, 13, 30, 0, 0), 
      userId: 2,
    },
    {
      id: 2, 
      title: "Second booking", 
      start: new Date(2021, 6, 8, 13, 30, 0, 0), 
      end: new Date(2021, 6, 8, 14, 30, 0, 0), 
    },
  ];

.html:

<mwl-calendar-week-view
      *ngSwitchCase="'week'"
      [viewDate]="viewDate"
      [events]="bookingsFromServer"
      (dayHeaderClicked)="clickedDate = $event.day.date"
      (hourSegmentClicked)="openCalModal($event)"
      [dayStartHour] = "8"
      [dayEndHour] = "18"
      [refresh]="refresh"
      [startDay] = "1"
      [weekStartsOn] = "1"
    >
    </mwl-calendar-week-view>

但是,當我從 server.ts 文件(使用 express node.js)獲取相同的事件時,它會崩潰並顯示以下錯誤:

Starting with v2.0.0-beta.1 date-fns doesn't accept strings as date arguments. Please use `parseISO` to parse strings.

Error
    at toDate (index.js:47)
    at startOfSecond (index.js:28)
    at isSameSecond (index.js:32)
    at isEventIsPeriod (calendar-utils.js:152)
    at calendar-utils.js:165
    at Array.filter (<anonymous>)
    at getEventsInPeriod (calendar-utils.js:164)
    at getWeekView (calendar-utils.js:361)
    at CalendarUtils.getWeekView (angular-calendar.js:1406)
    at CalendarWeekViewComponent.getWeekView (angular-calendar.js:2803)

如果我這樣寫我的日期:

start: new Date('2021-7-13T12:30:00.000Z'), 
end: new Date('2021-7-13T12:30:00.000Z'), 

表明:

angular-calendar.js:776 angular-calendar Event is missing the `start` property 
{id: 1, title: "First booking", start: null, end: null, userId: 2}
end: null
id: 1
start: null
title: "First booking"
userId: 2

有誰知道我該如何解決這個問題?

非常感謝

new Date('2021-7-13T12:30:00.000Z')產生一個無效的日期,這就是為什么你的startend屬性被設置為null

正確的字符串格式應該是:

new Date('2021-07-13T12:30:00.000Z') (月份使用 2 位數字)。

如果由於某種原因你需要堅持你原來的字符串格式,我建議你像這樣使用 date-fns 的parse函數:

start: parse('2021-7-13T12:30:00.000Z', "yyyy-M-dd'T'HH:mm:ss.SSSX", new Date())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM