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 7582193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:22:37+00:00 2026-05-30T18:22:37+00:00

In a Rails 3.2 app, I have a query defined to return all Event

  • 0

In a Rails 3.2 app, I have a query defined to return all Event items with :due_date equal to today.

@due_today = Event.where(:due_date => Time.now.beginning_of_day..Time.now.end_of_day)

I want to modify this to return all events due today and tomorrow.

What is the best way to do this?

I know there are several options available to me:

Date.tomorrow
Date.current.tomorrow
Date.now.tomorrow
DateTime.now.tomorrow.to_date
Time.now.tomorrow.to_date
Date.current+1

I’m sure there are others. Are all of these interchangeable? Are there any differences in performance? Are there any issues associated with different approaches? I’d welcome any suggestions as to which is the best way to do this.

For extra kudos: I also want to display the :due_date as Today HH:MM or Tomorrow HH:MM, where HH:MM is the time. Is there a method baked in to Rails to display dates as Today or Tomorrow? Or will I need to define my own scope?

Many thanks!

  • 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-30T18:22:38+00:00Added an answer on May 30, 2026 at 6:22 pm

    Benchmarking these, I get:

    N = 100000
    Benchmark.bmbm do |test|
      test.report("Date.tomorrow") do
        N.times do
          x = Date.tomorrow
        end
      end
      test.report("Date.current.tomorrow") do
        N.times do
          x = Date.current.tomorrow
        end
      end
      # test.report("Date.now.tomorrow") # => Coughs up an exception, Date.now doesn't exist!
      test.report("DateTime.now.tomorrow.to_date") do
        N.times do 
          x = DateTime.now.tomorrow.to_date
        end    
      end
      test.report("Time.now.tomorrow.to_date") do
        N.times do
          x = Time.now.tomorrow.to_date
        end
      end
      test.report("Date.current+1") do
        N.times do
          x = Date.current+1
        end
      end
      test.report("DateTime.tomorrow.to_date") do
        N.times do 
          x = DateTime.tomorrow.to_date
        end    
      end
    end
    

    Results:

    Rehearsal -----------------------------------------------------------------
    Date.tomorrow                   1.640000   0.010000   1.650000 (  1.662668)
    Date.current.tomorrow           1.580000   0.000000   1.580000 (  1.587714)
    DateTime.now.tomorrow.to_date   0.360000   0.010000   0.370000 (  0.363281)
    Time.now.tomorrow.to_date       4.270000   0.010000   4.280000 (  4.303273)
    Date.current+1                  1.580000   0.010000   1.590000 (  1.590406)
    DateTime.tomorrow               0.160000   0.000000   0.160000 (  0.164075)
    -------------------------------------------------------- total: 9.630000sec
    
                                        user     system      total        real
    Date.tomorrow                   1.590000   0.000000   1.590000 (  1.601091)
    Date.current.tomorrow           1.610000   0.010000   1.620000 (  1.622415)
    DateTime.now.tomorrow.to_date   0.310000   0.000000   0.310000 (  0.319628)
    Time.now.tomorrow.to_date       4.120000   0.010000   4.130000 (  4.145556)
    Date.current+1                  1.590000   0.000000   1.590000 (  1.596724)
    DateTime.tomorrow               0.140000   0.000000   0.140000 (  0.137487)
    

    From your list of suggestions, DateTime.now.tomorrow.to_date is faster.

    Check out the last option I’ve added though, it returns a Date object and is the fastest of the bunch by a country mile. It’s also one of the most human-readable from the list.

    Assuming you’re using MYSQL, your query might be faster if you use MySQL’s BETWEEN() function:

    @due_today = Event.where("due_date BETWEEN ? AND ?", DateTime.today, DateTime.tomorrow)
    

    Although I’m not sure if you have indexes on events.due_date or if BETWEEN will still use these. You’ll have to benchmark both to see which is quicker with a large DATA set.

    Hope that helps?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On my rails app I have a list of items (like a task list)
I have a Rails app whose major part is to query another web-service using
In my Rails app, I have the following PostgreSQL query to find the most
In a Rails app I have Sales and Salespeople: class Sale < ActiveRecord::Base belongs_to
I am creating a rails app and have used this code in one of
I have a rails app that I have serving up XML on an infrequent
I have a Rails app that I have successfully tested with Mongrel and Webkit.
I have a Rails app that I need to deploy. Here are the facts:
I have a rails app that has picked up a bit of traction, but
I have a Rails app that sets a cookie and does a redirect to

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.