DateTime 的 Tsx luxon 类型

发布于 2025-01-16 14:26:23 字数 1066 浏览 1 评论 0原文

我有 luxon 格式的日期,当我尝试从控制台打印信息时,它告诉我:

在此处输入图像描述

我收到以下错误 TS2339:属性 'c'不存在于“DateTime”类型上。

在此处输入图像描述

这是因为我声明最后一天的元素属于 DateTime 类型。

luxon 有自己可以导入的格式吗?

或者有没有办法为 luxon 创建数据时间类型,这样您就不会收到错误。

你能帮我个忙吗?

代码:

  const lNow = DateTime.now();
  const lThreeMonthsAgo = lNow.minus({month: 3}).startOf("week");
  let num = Math.ceil(lNow.diff(lThreeMonthsAgo, "days").days);
  let lastDays = [...Array(num).keys()].reduce(
      (acc, val) => [...acc, lThreeMonthsAgo.plus({day: val})],
      [] as Array<DateTime>
  );

  const month = lastDays.reduce((acc, val) => [...acc, val.c.month], [] as Array<number>);
  const unique_month = [...new Set(month)];

I have dates in luxon format, when I try to print information from the console it tells me this:

enter image description here

I get the following error TS2339: Property 'c' does not exist on type 'DateTime'.:

enter image description here

This is because I stated that last day elements are of type DateTime.

Does luxon have its own format that I can import?

Or is there a way to create a datatime type for luxon, so you don't get the error.

Can you give me a hand?

Code:

  const lNow = DateTime.now();
  const lThreeMonthsAgo = lNow.minus({month: 3}).startOf("week");
  let num = Math.ceil(lNow.diff(lThreeMonthsAgo, "days").days);
  let lastDays = [...Array(num).keys()].reduce(
      (acc, val) => [...acc, lThreeMonthsAgo.plus({day: val})],
      [] as Array<DateTime>
  );

  const month = lastDays.reduce((acc, val) => [...acc, val.c.month], [] as Array<number>);
  const unique_month = [...new Set(month)];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳钉梦 2025-01-23 14:26:23

我对 luxon 没有任何经验,但打字显示 yearmonth 等被声明为访问器,因此您可以直接在 yearmonth 等上对它们进行索引code>DateTime 对象,即将 val.c.month 替换为 val.month

<一href="https://www.typescriptlang.org/play?jsx=0#code/JYWwDg9gTgLgBAbwCIEMYFMAqp0F84BmUEIcA5ADYCuAHhAHZkCwAUKwMYMDO8FAchADucALxxUG bCHQA6ekIAUASgDcHbr0walkonQBZBjC1cAggHMIouPyEyQwelS4KEII1oBccAMy4lMjwosADyBAoARIJ6ANYRqqwU6PBOpGL6aFoy7OjAFAq2gjiIAJsAE4RTaugYeZpYA NHARxSgAnlzxJW1cCSxJvCg8qO3WANoyE6ZQUG0KqQEx6O3KALoyusVUOQqscHtwCijs7I0AbigUSqIAfHDjE0cnNlV6hvTGdRAyYNQuCC2tbznCj%20Fb1Xb7UYrOCDO BTGatAA8EiwOGurF66noPDg7neWmsFEGMGGXHW6E220OxzOFyuIlu9xkj1pFHsHjBd2hsPhbURqQARugoNdepxsfAqPRgABHKjoAD6eOMYwmcnQwgAyskFMqtEoVmoWEA" rel="nofollow noreferrer">TypeScript 游乐场

I don't have any experience with luxon, but the typings show that year, month, etc. are declared as accessors, so you can just index directly them on a DateTime object, i.e. replace val.c.month with val.month.

TypeScript playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文