首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打字本: getTime不是函数

打字本: getTime不是函数
EN

Stack Overflow用户
提问于 2021-06-22 08:26:56
回答 1查看 73关注 0票数 0

我正在使用打字本作为我的功能。我在数据库中有两组数据,一组是date,一组是time。两人都在期待字符串。

当用户选择日期时,我发出了一个POST请求,该时间将为用户选择。我为此做了一个助手函数,当交付时间开始时,这意味着用户选择时间已经过期,在这种情况下,我会在前端显示一个警告。

“您选择的时间过期了!”。

我试图在函数中传递像这个now = new Date()这样的默认日期参数,而不是直接使用new Date()。当我控制台日志typeof now时,它显示参数是函数。

PS:我在打字稿上还是新手。如果有人告诉我关于我的错误的细节,对我来说将是很好的学习。

这是我的函数抛出错误

代码语言:javascript
复制
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;
};

,这一个工作,

代码语言:javascript
复制
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;
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-22 10:04:41

必须将参数now键入日期,否则参数的类型为any

代码语言:javascript
复制
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;
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68080071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档