Home > Software design >  Insert Hour and Minute to mongoDB date in node js
Insert Hour and Minute to mongoDB date in node js

Time:09-22

I am trying to insert a date with user specific time. User will select any time say, 08:30 And I need to insert that time to mongodb with the current date. I am trying to insert it like this

new Date().setHours(08,30)

but this gives me something like this 1632272411458. And I need it to get inserted in the below format

2021-09-22T08:30:00.00 00:00

CodePudding user response:

Use it like

var date = new Date();
date.setHours(08, 30);

CodePudding user response:

When working with date/time values then I always recommend the moment.js library. Would be this:

moment().startOf('minute').set({ hour: 8, minutes: 30 }).toDate()
  • Related