I have one object
objTime = {
"scheduleStartDate": "2022-07-13T12:00:00.540 00:00",
"slotDuration":30,
"noOfSlots":5,
}
I have to add scheduleEndDate dynamically by using slotDuraion key and noOfSlots is my loop length.
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")}`
);
}
But it not works as accept I want o/p like this.
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:00
Any moment.js function for same I am using NodeJS. Thanks in advance.
CodePudding user response:
just a little tweak to your implementation to manipulate the date time using the i
value
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>