Updating the newMessage property

If you remember, on message we have a createdAt property for both newMessage and newLocationMessage. All we need to do is get that value, pass it in to moment, and then generate our formatted string.

We can make a new variable called formattedTime, and we can set this equal to a call to moment passing in the timestamp, message.createdAt:

socket.on('newMessage', function (message) {  var formattedTime = moment(message.createAt)

Now we can go ahead and do whatever we like. We could call format passing in the exact same string we use over in time.js, the hour, the minutes and the am/pm; h:, two lowercase ms, followed by a space and a lowercase a:

var formattedTime = moment(message.createdAt).format('h:mm a'); ...

Get Advanced Node.js Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.