typescript - 查找两个日期的最小值/最大值(键入)
问题描述
我想要一个强类型代码。当我应用类似问题的解决方案时 -数组中日期的最小值/最大值?- 我得到错误
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 中查找最小/最大日期的正确方法是什么?
解决方案
您可以在打字稿中像这样比较日期:
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
返回一个数字。
推荐阅读
- django - 通过 Nginx (Django/React/Nginx/Docker-Compose) 提供 Django 媒体文件
- sql-server - 从存储过程插入不同的列
- gradle - Gradle 错误 - “构建失败:无法解析配置 ':compileClasspath' 的所有依赖项”
- c# - 为什么相机不旋转以面对第一个航路点?
- html - 图像上的阴影
- ms-access - 查询比较以查找匹配项
- javascript - 如何在功能组件中访问 DOM
- ansible - 如何使用由剧本创建的 Ansible 库存文件?
- python - 计算事件持续时间 pandas
- r - 按日期和唯一 ID 分组的滚动 7 天总和