js get quater hours in time range

// Start and End is the UTC-Timestamp
function getQuaterHours(start: number, end: number): Date[] {
    const difference = end - start;

  // Return if Difference between start and end is 0
    if (difference <= 0) {
      // You can return a default range if difference is 0
        return [];
    }

  // Maybe you have to play a bit between .getUTCHours and .getHours- Method
    const hours = new Date(difference).getUTCHours();

    const startMinutes = new Date(start).getMinutes();
    const endMinutes = new Date(end).getMinutes();

  // Describes 4 Quaters / Hour * Hours in Difference
  // between start and end
    const factor = hours * 4 + Math.round(startMinutes / 15) + Math.round(endMinutes / 15)

    const results: Date[] = [];

  // For each element, round to next Higher quater
    for (let index = 0; index <= factor; index++) {
        const time = new Date(start + (index * difference / factor));
        time.setMinutes(roundToNextQuater(time.getMinutes()));
        results.push(new Date(time));
    }

    return results;
}

// Function to round to next quater
// If you want to round to next lower quater, use .floor()
// instead of .ceil()
function roundToNextQuater(minutes: number): number {
    return (Math.ceil(minutes / 15) * 15) % 60;
}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source