Home > Software design >  Tsx luxon type of DateTime
Tsx luxon type of DateTime

Time:03-25

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

CodePudding user response:

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

  • Related