Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 953541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:04:07+00:00 2026-05-16T00:04:07+00:00

how can i get a list of DateTime objects which match the following criteria:

  • 0

how can i get a list of DateTime objects which match the following criteria:

  • they are between two instances of DateTime
  • they fall on a fraction of an hour/min. eg. they are a full quarter of an hour

with active support a possible solution is:

(my_datetime_ob_a.to_i .. my_datetime_ob_b.to_i).each { |timestamp|
  puts timestamp if (timestamp % (3600/4) == 0)
}

this is not nice: too many iterations and too much conversion (you would need to reconvert timestamp to a DateTime object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:04:07+00:00Added an answer on May 16, 2026 at 12:04 am

    Here is an alternative approach:

    • Find the first timestamp >= my_datetime_ob_a which is a full quarter of an hour (i.e. round up to 15 minute interval)
    • While this timestamp is <= my_datetime_ob_b put it in your list, add 15 minutes and loop

    Implementation:

    def round_to_quarter_hour(datetime)
      secs_past_quarter_hour = datetime.to_i % 15.minutes
      if secs_past_quarter_hour == 0
        datetime
      else
        secs_to_advance_to_next_quarter_hour = 15.minutes - 
          secs_past_quarter_hour
        datetime.advance :seconds => secs_to_advance_to_next_quarter_hour
      end
    end
    
    def quarter_hours_between(datetime_a, datetime_b)
      results = []
      datetime = round_to_quarter_hour(datetime_a)
      while datetime <= datetime_b
        results << datetime
        datetime = datetime.advance :minutes => 15
      end
      results
    end
    

    Example:

    >> d1
    => Sat, 24 Jul 2010 17:28:06 +0100
    >> d2
    => Sat, 24 Jul 2010 18:03:00 +0100
    >> quarter_hours_between(d1, d2)
    => [Sat, 24 Jul 2010 17:30:00 +0100, Sat, 24 Jul 2010 17:45:00 +0100,
    Sat, 24 Jul 2010 18:00:00 +0100]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List<> of objects containing two strings and a DateTime. I want
You can get a list of databases using PRAGMA database_list or a list of
There's a function in PHP where you can get a list of the user
I have one table having ID and other attributes. How can I get list
I am building a pretty basic form app. I can get a list of
Is there any debugging options built-in in the Xlib (libX11.so)? Can I get list
Can I get a list of the playlists and songs in each playlist in
Where can I get a list of browser differences in the implementation of DOM?
How can I to get list of all wcf services running on a machine?
How can I get the list of cross product pairs from a list of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.