简体   繁体   English

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

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

I'm using https://github.com/mattlewis92/angular-calendar on my Ionic app.我在我的 Ionic 应用程序上使用https://github.com/mattlewis92/angular-calendar When I'm loading an array of events from my component file it's all good.当我从组件文件加载一系列事件时,一切都很好。 This is how I do it:这就是我的做法:

.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: .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>

But when I get the same events from my server.ts file (using express node.js) it crashes showing these errors:但是,当我从 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)

And if I write my dates like this:如果我这样写我的日期:

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

it shows:表明:

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

Anyone knows how can I solve this issue?有谁知道我该如何解决这个问题?

Thanks a lot非常感谢

new Date('2021-7-13T12:30:00.000Z') produces an invalid date, that's why you're start and end properties are set to null . new Date('2021-7-13T12:30:00.000Z')产生一个无效的日期,这就是为什么你的startend属性被设置为null

The correct string format should be:正确的字符串格式应该是:

new Date('2021-07-13T12:30:00.000Z') (use 2 digits for months). new Date('2021-07-13T12:30:00.000Z') (月份使用 2 位数字)。

If for some reason you need to stick to your original string format, I suggest you use the parse function of date-fns like this:如果由于某种原因你需要坚持你原来的字符串格式,我建议你像这样使用 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