date.toLocaleTimeString 不是函数

date.toLocaleTimeString is not a function

我正在尝试打印来自 mongodb 的 script.js 的日期,但出现以下错误

未捕获类型错误:date.toLocaleTimeString 不是函数

function db_old_msgs(old_msg) {
    $('.chat-messages').append('<span class="msg"><b>' + old_msg.nick + ': </b>' + old_msg.msg +" "+FormatTime(old_msg.created) + "</span><br/>");
}

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

我在没有像“+ old_msg.created.toLocaleTimeString() +”这样的 FormatTime 函数的情况下尝试了上面的方法,但我得到了同样的错误。

从数据库接收到的时间格式为 2021-12-16T13:22:01.600+00:00。

来自 server.js

的日期显示正确
socket.broadcast.emit('msg', {from: users[socket.id],message: message, time:newMsg.created.toLocaleTimeString()})

非常感谢

日期是来自服务器的字符串,因此我必须将其转换为日期,然后将函数传递给 LocaleTimeString()

function db_old_msgs(old_msg) {
    var dbtime=new Date(old_msg.created);
    var formattedDate = dbtime.toLocaleTimeString();
    $('.chat-messages').append('<span class="msg"><b>' + old_msg.nick + ': </b>' + old_msg.msg +" "+formattedDate + "</span><br/>");
}