【发布时间】:2021-03-05 03:47:21
【问题描述】:
我需要计算两个日期之间的天数差。
最后我需要知道某个日期是否已过期。
但我想不出解决办法。
expiredAt 字段的类型为 datetime
Service.ts
import CheckLicenses from "../../helpers/CheckLicenses";
await CheckLicenses("valor");
CheckLicenses.ts
import License from "../models/License";
import AppError from "../errors/AppError";
import { logger } from "../utils/logger";
const CheckLicenses = async (company: string): Promise<boolean> => {
const license = await License.findOne({
where: { company }
});
if (!license) {
throw new AppError("ERR_NO_LICENCE_FOUND", 404);
} else {
const { expiredAt } = license;
const today = new Date();
if (expiredAt <= today) throw new AppError("EXPIRED", 401);
}
return true;
};
export default CheckLicenses;
【问题讨论】:
-
expiredAt 的类型是什么?时间戳编号或日期
-
类型为日期时间
-
哇,新类型。让我们试试
if (expiredAt.valueOf() <= today.valueOf())
标签: node.js typescript