简体   繁体   English

DateTime 的 Tsx luxon 类型

[英]Tsx luxon type of DateTime

I have dates in luxon format, when I try to print information from the console it tells me this:我有luxon格式的日期,当我尝试从控制台打印信息时,它告诉我:

在此处输入图像描述

I get the following error TS2339: Property 'c' does not exist on type 'DateTime'.我收到以下错误TS2339: Property 'c' does not exist on type 'DateTime'. : :

在此处输入图像描述

This is because I stated that last day elements are of type DateTime .这是因为我声明最后一天的元素是DateTime类型。

Does luxon have its own format that I can import? luxon 有自己的格式可以导入吗?

Or is there a way to create a datatime type for luxon, so you don't get the error.或者有没有办法为 luxon 创建数据时间类型,这样就不会出现错误。

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)];

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, ie replace val.c.month with val.month .我对 luxon 没有任何经验,但输入显示yearmonth等被声明为访问器,因此您可以直接在DateTime object 上索引它们,即用val.c.month替换val.month

TypeScript playground TypeScript 操场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM