Karl Bateman Karl Bateman - 1 year ago 44
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

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

Answer Source

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

let newDate = new moment()

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())