F
F
Fatima Askerova2021-10-05 11:44:02
typescript
Fatima Askerova, 2021-10-05 11:44:02

How to divide start and end time by given interval?

There are 3 inputs. The first one takes the start time of work, the second time the end of the working day, the third one I enter by what interval I want to divide this period of time. For example, my working day starts at 10.00 and ends at 18.00, I can see patients every 30 minutes. Need to:
10.00-10.30
10.30-11.00
.
.
.
17.00-17.30
17.30-18.00

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-10-05
@Seasle

const start = 11 * 60 * 60;
const end = 18 * 60 * 60;
const step = 30 * 60;
const count = Math.floor((end - start) / step);
const result: string[] = [];
const date = new Date();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);

const format = (date: Date) => date.toLocaleTimeString('ru-RU', { hour: '2-digit', minute: '2-digit' });

for (let index = 0; index < count; index++) {
  const from = new Date(date.getTime() + (start + step * index) * 1000);
  const to = new Date(date.getTime() + (start + step * (index + 1)) * 1000);
  
  result.push(`${format(from)} - ${format(to)}`);
}

console.log(result);
/* [ '11:00 - 11:30', '11:30 - 12:00', '12:00 - 12:30', '12:30 - 13:00', '13:00 - 13:30',
     '13:30 - 14:00', '14:00 - 14:30', '14:30 - 15:00', '15:00 - 15:30', '15:30 - 16:00',
     '16:00 - 16:30', '16:30 - 17:00', '17:00 - 17:30', '17:30 - 18:00' ] */

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question