Karl Bateman Karl Bateman - 4 months ago 12
Node.js Question

Creating a new date using momentjs within a nodejs consumer

I have a NodeJS RabbitMQ consumer which creates a date using MomentJS. However because this runs constantly, the date is always the same.

How can I create a new date every time the consumer receives a message?
Should I wrap the call to

moment.format()
within a function or do I need to use
new moment.format()
?

Answer

This could be done by initializing a new date when receiving a new message like

let newDate = new moment()
console.log(newDate.unix())

Edit: Also working without the new keyword

let newDate = moment()

but better for readability and preventing some possible strange effects

let newDate = moment(new Date())
Comments