Replies: 1 comment
-
This is a good use case for the https://date-fns.org/docs/eachDayOfInterval import { eachDayOfInterval, eachMinuteOfInterval, lightFormat } from "date-fns";
// inputs
const intervalStepInMinutes = 30;
const startDateString = "2022-10-22";
const endDateString = "2022-10-24";
const startTimeString = "08:00";
const endTimeString = "17:00";
// parse time bounds to integers
const [startHours, startMinutes] = startTimeString
.split(":")
.map((v) => parseInt(v, 10));
const [endHours, endMinutes] = endTimeString
.split(":")
.map((v) => parseInt(v, 10));
// find all days in interval
const start = new Date(`${startDateString}T00:00`);
const end = new Date(`${endDateString}T00:00`);
const days = eachDayOfInterval({ start, end });
const results = [];
for (const day of days) {
// create start and end datetime for each day
const start = new Date(day.getTime());
start.setHours(startHours, startMinutes, 0, 0);
const end = new Date(day.getTime());
end.setHours(endHours, endMinutes, 0, 0);
// find 30 min intervals within time bounds
const dailyResults = eachMinuteOfInterval(
{ start, end },
{ step: intervalStepInMinutes }
);
results.push(...dailyResults);
}
// convert resulting dates to desired string format
const formattedResults = results.map((d) =>
lightFormat(d, "yyyy-MM-dd'T'HH:mm")
); And here's a way to check the time range, per your original question and example. import { differenceInMinutes, addMinutes, lightFormat } from "date-fns";
// inputs
const intervalInMinutes = 30;
const startDate = "2022-10-22";
const endDate = "2022-10-24";
const startingTime = "08:00";
const endingTime = "17:00";
const [startHours, startMinutes] = startingTime
.split(":")
.map((v) => parseInt(v, 10));
const [endHours, endMinutes] = endingTime
.split(":")
.map((v) => parseInt(v, 10));
const minutesLowBound = startHours * 60 + startMinutes;
const minutesHighBound = endHours * 60 + endMinutes;
const appointmentStartFrom = new Date(`${startDate}T${startingTime}`);
const appointmentsEndAt = new Date(`${endDate}T${endingTime}`);
const hoursDifference = differenceInMinutes(
appointmentsEndAt,
appointmentStartFrom
);
const appointmentsToCreate = hoursDifference / intervalInMinutes;
const appointmentsCreated = [];
if (Number.isInteger(appointmentsToCreate)) {
for (let i = 0; i <= appointmentsToCreate; i++) {
const date = addMinutes(appointmentStartFrom, intervalInMinutes * i);
const minutes = date.getHours() * 60 + date.getMinutes();
if (minutes >= minutesLowBound && minutes <= minutesHighBound) {
appointmentsCreated.push(lightFormat(date, "yyyy-MM-dd'T'HH:mm"));
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi all,
My desired array of objects should look something like this:
[ ... "2022-10-22T12:30", "2022-10-22T13:00", "2022-10-22T13:30", "2022-10-22T14:00", ... ]
I am working with:
"2022-10-22"
"2022-10-24"
"08:00"
"17:00"
My question is, how do I determine if the date time
"2022-10-22T18:00"
is outside of the desired range, so this date should not get pushed into the array, but"2022-10-23T08:00"
and dates after should be added again.What I have so far is:
Beta Was this translation helpful? Give feedback.
All reactions