date.toLocaleDateString不是函数

Ale*_*ich 9 javascript string date node.js botframework

有简单的函数返回错误:

错误:date.toLocaleDateString不是函数

TypeError: date.toLocaleDateString is not a function
    at FormatTime (../Src/rootdialog.js:87:58)
Run Code Online (Sandbox Code Playgroud)

功能定义:

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

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

PS问题是由BotBuilder架构引起的.

Vip*_*oni 15

您可以使用

new Date(date).toLocaleDateString();
Run Code Online (Sandbox Code Playgroud)


Ber*_*rgi 12

Date.parse返回一个数字.你在找new Date.或者,如果time已经是一个Date实例,只需使用time.toLocaleDateString()(并确保它确实在每次调用函数时)!

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


Ogg*_*las 5

在 React 应用程序中遇到此错误,如下解决:

{ (item.created instanceof Date) ? item.created.toLocaleDateString() : new Date(item.created).toLocaleDateString() }
Run Code Online (Sandbox Code Playgroud)