DateTime 的 Tsx luxon 类型
我有 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:
I get the following error TS2339: Property 'c' does not exist on type 'DateTime'.
:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 luxon 没有任何经验,但打字显示
year
、month
等被声明为访问器,因此您可以直接在year
、month
等上对它们进行索引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 aDateTime
object, i.e. replaceval.c.month
withval.month
.TypeScript playground