luxon

Divide hours interval in minutes with Luxon


How can I divide an hours interval in minutes with Luxon? Example

starTime = 06:00
endaTime = 12:00
interval = 00:30

["06:00AM", "06:30AM", "07:00AM", "07:30AM", "08:00AM", "08:30AM", "09:00AM", "09:30AM", "10:00AM", "10:30AM", "11:00AM", "11:30AM"]

Solution

  • You can simply:

    Example:

    const DateTime = luxon.DateTime;
    const Duration = luxon.Duration;
    const startTime = "06:00"
    const endTime = "12:00"
    const interval = "00:30"
    
    const dtStart = DateTime.fromFormat(startTime, "HH:mm");
    const dtEnd = DateTime.fromFormat(endTime, "HH:mm");
    const durationInterval = Duration.fromISOTime(interval);
    
    let res = [];
    let i = DateTime.fromFormat(startTime, "HH:mm");
    while (i < dtEnd) {
      res.push(i.toFormat("HH:mma"));
      i = i.plus(durationInterval);
    }
    console.log(res);
    <script src="https://cdn.jsdelivr.net/npm/luxon@1.26.0/build/global/luxon.js"></script>