查找两个日期的最小值/最大值(键入)

问题描述 投票:-2回答:1

我想要一个强类型代码。当我使用类似问题的解决方案时-Min/Max of dates in an array?-我收到错误

TS2345: Argument of type 'Date' is not assignable to parameter of type 'number'.

我的代码

  const min: Date = Math.min(begin as Date, (end || defaultDate) as Date);
  const max: Date = Math.max(begin as Date, (end || defaultDate) as Date);

begin as Date部分带有下划线。

在打字稿中查找最小/最大日期的正确方法是什么?

typescript date max min
1个回答
0
投票

您可以在打字稿中像这样比较日期:

const begin: Date = new Date();
const end: Date = new Date();

const min: Date = begin < end ? begin : end;
const max: Date = begin > end ? begin : end;

您遇到的问题是Math.min返回一个数字。

© www.soinside.com 2019 - 2024. All rights reserved.