我正在使用打字本作为我的功能。我在数据库中有两组数据,一组是date,一组是time。两人都在期待字符串。
当用户选择日期时,我发出了一个POST请求,该时间将为用户选择。我为此做了一个助手函数,当交付时间开始时,这意味着用户选择时间已经过期,在这种情况下,我会在前端显示一个警告。
“您选择的时间过期了!”。
我试图在函数中传递像这个now = new Date()这样的默认日期参数,而不是直接使用new Date()。当我控制台日志typeof now时,它显示参数是函数。
PS:我在打字稿上还是新手。如果有人告诉我关于我的错误的细节,对我来说将是很好的学习。
这是我的函数抛出错误
const isStartimeExpired = (
date: string,
time: string,
now = new Date(),
) => {
const [startTime] = time.split(' - ');
const newDate = new Date(`${date}T${startTime}`);
if (newDate.getTime() < now.getTime()) { // this throws an error that getTime() is not function
return true;
} else if (isEmptyString(date) || isEmptyString(time)) {
return false;
}
return false;
};,这一个工作,
const isStartimeExpired = (
date: string,
time: string,
) => {
const [startTime] = time.split(' - ');
const newDate = new Date(`${date}T${startTime}`);
if (newDate.getTime() < new Date().getTime()) { // this one does not throw any error
return true;
} else if (isEmptyString(date) || isEmptyString(time)) {
return false;
}
return false;
};发布于 2021-06-22 10:04:41
必须将参数now键入日期,否则参数的类型为any。
const isStartimeExpired = (
date: string,
time: string,
now: Date = new Date(),
) => {
const [startTime] = time.split(' - ');
const newDate = new Date(`${date}T${startTime}`);
if (newDate.getTime() < now.getTime()) {
return true;
} else if (isEmptyString(date) || isEmptyString(time)) {
return false;
}
return false;
};https://stackoverflow.com/questions/68080071
复制相似问题