Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
date.toLocaleTimeString is not a function

Am trying to print the date from script.js coming from mongodb but am getting the below error

Uncaught TypeError: date.toLocaleTimeString is not a function

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()  : "");
}

I tried the above without the function FormatTime like "+ old_msg.created.toLocaleTimeString() +" but I get the same error.

The time format that is received from db is 2021-12-16T13:22:01.600+00:00.

Dates are being displayed correctly which are coming from server.js

socket.broadcast.emit('msg', {from: users[socket.id],message: message, time:newMsg.created.toLocaleTimeString()})

Many thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The date was coming as string from the server so I had to convert it to date and then pass the function toLocaleTimeString()

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/>");
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda