【问题标题】:TypeError: c.diff is not a function and Property 'diff' does not exist on type 'Date'TypeError: c.diff 不是函数并且属性 \'diff\' 在类型 \'Date\' 上不存在
【发布时间】:2022-12-07 18:37:24
【问题描述】:

我在我的 google chrome 控制台中遇到了一些问题,我收到了 Uncaught TypeError: c.diff is not a function,而在我的 vscode 中,当我将鼠标悬停在我的代码中时,我收到了 'diff' 在 'Date' 中不存在。但是当我在其他代码编辑器的代码示例中测试我的代码时,我收到了预期的输出。这是将 moment 转化为 luxon 的过程。这是代码示例:https://stackblitz.com/edit/js-nw6ctg?file=index.js。但我在这里导入了 luxon:

   import * as luxon from 'luxon';

这是我的代码示例,我将其悬停在 vscode 中并给出错误:

  const checkInLuxon5K = DateTime.now().startOf('day').set({ hour: 14 });
  const checkOutLuxon5K = DateTime.now()
  .startOf('day')
  .plus({ days: 2 })
  .set({ hour: 11 });

const checkIn = DateTime.fromISO(checkInString, { zone: 'Europe/Spain' }).toJSDate();
const checkOut = DateTime.fromISO(checkOutString, { zone: 'Europe/Spain' }).toJSDate();

  const totalDuration = Duration.fromMillis((checkOut).diff(checkIn)).as('minutes');
  const currentDuration = Duration.fromMillis((checkIn).diff(DateTime.now())).as('minutes');
Output: Error: checkIn.diff is not a function

【问题讨论】:

    标签: javascript reactjs typescript momentjs luxon


    【解决方案1】:

    你的时区欧洲/西班牙是无效的

    用这个

    const checkInGoL = DateTime.fromISO(checkInLuxon5K, {
      zone: 'Europe/Lisbon', //or any timezone listed below
    })
    

    你可以在这里获得完整的时区列表:complete list of timezones

    【讨论】:

    • 谢谢@Ragnar 我在这里仍然遇到错误 const checkInGoL = ``` DateTime.fromISO(checkInLuxon5K, { zone: 'Europe/Lisbon', }).toJSDate(); console.log(This luxon Method 10 : , Duration.fromMillis(checkInGoL.diff(DateTime.now())).as('分钟'));```
    • 错误:checkInGoL.diff 不是函数 @Ragnar
    猜你喜欢
    • 2022-10-16
    • 1970-01-01
    • 2017-10-21
    • 2020-08-02
    • 2018-09-21
    • 2021-11-30
    • 2021-09-03
    • 2016-12-19
    • 1970-01-01
    相关资源
    最近更新 更多