Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!