I have UTC dates stored in a collection like so:
"orderBeforeStartTime" : ISODate("2021-03-20T14:00:00.000Z"),
"orderBeforeEndTime" : ISODate("2021-03-20T16:00:00.000Z")
I want to do a loop that starts at orderBeforeStartTime
and ends at orderBeforeEndTime
to populate an array with time intervals.
So for example using the above data, I should get an array as such:
['14:00', '14:30', '15:00', '15:30', '16:00'];
There should be 30 minutes interval between each number until I reach orderBeforeEndTime
.
My code:
const { DateTime } = require("luxon");
let startDate;
const endDate = DateTime.fromHTTP(
item.timeSegment.orderBeforeEndTime.toUTCString()
);
if (new Date() > item.timeSegment.orderBeforeStartTime) {
startDate = DateTime.utc();
} else {
startDate = DateTime.fromHTTP(
item.timeSegment.orderBeforeStartTime.toUTCString()
);
}
while (startDate <= endDate) {
item.orderTimeSegments.push(
startDate.toLocaleString(DateTime.TIME_24_SIMPLE)
);
startDate = startDate.plus({ minutes: 30 });
}
Is this the correct way to do it?
I have UTC dates stored in a collection like so:
"orderBeforeStartTime" : ISODate("2021-03-20T14:00:00.000Z"),
"orderBeforeEndTime" : ISODate("2021-03-20T16:00:00.000Z")
I want to do a loop that starts at orderBeforeStartTime
and ends at orderBeforeEndTime
to populate an array with time intervals.
So for example using the above data, I should get an array as such:
['14:00', '14:30', '15:00', '15:30', '16:00'];
There should be 30 minutes interval between each number until I reach orderBeforeEndTime
.
My code:
const { DateTime } = require("luxon");
let startDate;
const endDate = DateTime.fromHTTP(
item.timeSegment.orderBeforeEndTime.toUTCString()
);
if (new Date() > item.timeSegment.orderBeforeStartTime) {
startDate = DateTime.utc();
} else {
startDate = DateTime.fromHTTP(
item.timeSegment.orderBeforeStartTime.toUTCString()
);
}
while (startDate <= endDate) {
item.orderTimeSegments.push(
startDate.toLocaleString(DateTime.TIME_24_SIMPLE)
);
startDate = startDate.plus({ minutes: 30 });
}
Is this the correct way to do it?
Share Improve this question edited Mar 19, 2021 at 17:23 Exhaler asked Mar 19, 2021 at 16:56 ExhalerExhaler 1152 silver badges10 bronze badges 2-
Have you read the documentation? There's a
plus
method that will add a duration to the date. Make a duration of 30 minutes, add it toorderBeforeStartTime
until it beesorderBeforeEndTime
... – Heretic Monkey Commented Mar 19, 2021 at 17:05 - Thanks, I updated my post with the code to check if my logic is sound – Exhaler Commented Mar 19, 2021 at 17:22
1 Answer
Reset to default 7Based on the documentation, there is a way to iterate on the Interval given a DurationObjectUnits
.
const start = new Date("2021-03-20T14:00:00.000Z");
const end = new Date("2021-03-20T16:00:00.000Z");
const interval = Interval.fromDateTimes(
DateTime.fromJSDate(start),
DateTime.fromJSDate(end)
);
const desiredArray = interval.splitBy({ minutes: 30 }).map((d) => d.start);
Possible splits
export interface DurationObjectUnits {
year?: number | undefined;
years?: number | undefined;
quarter?: number | undefined;
quarters?: number | undefined;
month?: number | undefined;
months?: number | undefined;
week?: number | undefined;
weeks?: number | undefined;
day?: number | undefined;
days?: number | undefined;
hour?: number | undefined;
hours?: number | undefined;
minute?: number | undefined;
minutes?: number | undefined;
second?: number | undefined;
seconds?: number | undefined;
millisecond?: number | undefined;
milliseconds?: number | undefined;
}