Ionic 错误:date-fns不接受字符串作为日期参数,请使用`parseISO`解析字符串

uyhoqukh  于 2023-09-28  发布在  Ionic
关注(0)|答案(2)|浏览(141)

我在我的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

有谁知道如何解决这个问题吗?
非常感谢

hs1ihplo

hs1ihplo1#

new Date('2021-7-13T12:30:00.000Z')产生一个无效的日期,这就是为什么你的startend属性设置为null
正确的字符串格式应为:
new Date('2021-07-13T12:30:00.000Z')(月份使用两位数字)。
如果出于某种原因,你需要坚持原来的字符串格式,我建议你像这样使用date-fns的parse函数:

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

34gzjxbg2#

在我的例子中,在reactjs中,我通过使用const dateDob = Date.parse('2021 -7- 13 T12:30:00.000Z ')得出结论,它以毫秒为单位返回Date,然后用途:
new Date(dateDob)

相关问题