tengo un objeto
objTime = { "scheduleStartDate": "2022-07-13T12:00:00.540+00:00", "slotDuration":30, "noOfSlots":5, }Tengo que agregar scheduleEndDate dinámicamente usando la clave slotDuraion y noOfSlots es la longitud de mi bucle.
for (let i = 0; i < objTime.noOfSlots; i++) { let startDateTime = moment(objTime.scheduleStartDate); let expectedEndDateTime = moment(startDateTime).add(objTime.slotDuration, "minutes"); console.log( `Start : ${moment(startDateTime).format("DD-MM-YYYY hh:mm:ss")} | End : ${moment(expectedEndDateTime).format("DD-MM-YYYY hh:mm:ss")}` ); }Pero no funciona como aceptar Quiero o/p como este.
Start : 13-07-2022 05:30:00 | End : 13-07-2022 06:00:00 Start : 13-07-2022 06:00:00 | End : 13-07-2022 06:30:00 Start : 13-07-2022 06:30:00 | End : 13-07-2022 07:00:00 Start : 13-07-2022 07:00:00 | End : 13-07-2022 07:30:00 Start : 13-07-2022 07:30:00 | End : 13-07-2022 08:00:00Cualquier función moment.js para la misma estoy usando NodeJS. Gracias por adelantado.
solo un pequeño ajuste a su implementación para manipular la fecha y hora usando el valor i
const objTime = { "scheduleStartDate": "2022-07-13T12:00:00.540+00:00", "slotDuration": 30, "noOfSlots": 5, } for (let i = 0; i < objTime.noOfSlots; i++) { let startDateTime = moment(objTime.scheduleStartDate).add(objTime.slotDuration * (i), "minutes"); let expectedEndDateTime = moment(objTime.scheduleStartDate).add(objTime.slotDuration * (i + 1), "minutes"); console.log(`Start : ${moment(startDateTime).format("DD-MM-YYYY hh:mm:ss")} | End : ${moment(expectedEndDateTime).format("DD-MM-YYYY hh:mm:ss")}`); } <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>