javascript - date.toLocaleDateString 不是函数

标签 javascript node.js string date botframework

具有返回错误的简单函数:

ERROR: date.toLocaleDateString is not a function

TypeError: date.toLocaleDateString is not a function
    at FormatTime (../Src/rootdialog.js:87:58)

函数定义:

function FormatTime(time, prefix = "") {
    var date = Date.parse(time);
    return ((typeof time != "undefined") ? prefix + date.toLocaleDateString()  : "");
}

函数接收 Date 对象作为输入,但即使使用 Date.parse() 显式转换为 Date 也无济于事。使用 Node.js 8.x。有什么解决办法吗?

P.S. Issue was caused by BotBuilder architecture.

最佳答案

Date.parse返回一个数字。您正在寻找new Date .或者,如果 time 已经是 Date 实例,只需使用 time.toLocaleDateString()(并确保它确实存在于对函数的每次调用中)!

function formatTime(time, prefix = "") {
    return typeof time == "object" ? prefix + time.toLocaleDateString() : "";
}

关于javascript - date.toLocaleDateString 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45724975/

相关文章:

javascript - 在jquery中创建一个div slider

javascript - jquery gettime总是使时间早于4小时

node.js - NestJS 将 @Query() 反序列化为具有复杂类型的 DTO

java - Java 中的字符串构建性能

c++ - Valgrind 在使用字符串类型成员时识别内存泄漏(使用 nvcc 编译)

c# - 不会将字符串转换为十进制 c#(输入字符串的格式不正确。)

javascript - 使用clearInterval方法后如何再次运行setInterval函数? - JavaScript

node.js - 我对 Node 中 'Non-blocking' 的理解是否正确?

javascript - 如何在本地离线开发Meteor.js应用程序?

javascript - 将包含一个元素的 jQuery 对象称为节点在语义上是否不正确?