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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:04:38+00:00 2026-05-30T20:04:38+00:00

In my view, I have two date fields like so: <%= f.text_field :start_date %>

  • 0

In my view, I have two date fields like so:

<%= f.text_field :start_date %>
<%= f.text_field :end_date %>

(I’m using the jquery-ui datepicker to set the dates, but left that code out for clarity. Dates are stored in the db as ‘2012-03-02’.)

And I calculate the difference between the two dates with a helper method and display it as #duration:

<div id="duration">
    <%= time_diff_in_natural_language(@project.start_date, @project.end_date) %>
</div>

This works as expected – it calculates the difference between the two dates stored in the database. But I’d really like the duration to recalculate on the fly – I’m guessing this would work via a javascript onchange(); event on the date fields that triggers a recalculation of the content in the duration field, but I’m stuck on how to make it happen. Can anyone point me to a good way to accomplish this?

(This question is really similar and I think that answer could work here too, but I can’t figure out how to display the time difference correctly since that would have to reference the rails helper method.)

  • 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-30T20:04:40+00:00Added an answer on May 30, 2026 at 8:04 pm

    one approach would be to make an ajax call to get the duration.

    so in your controller you would define an action called ‘duration’ for example, which would look for two params, :start_date and :end_date and simply call your helper method time_diff_in_natural_language. Something in the lines of:

    def duration
      time_diff_in_natural_language(params[:start_date], params[:end_date])
    end
    

    Then you can wire up the onSelect event of your date pickers (or the onChange event of the inputs) to call a javascript function, which would simply make an ajax call to the ‘duration’ action in your controller and set the #duration on success. Something like this:

    function get_duration() {
       $.ajax({
          url: '/duration',
          dataType: "json",
          type: "POST",
          data: { start_date: $("#project_start_date").val(), end_date: $("#project_end_date").val() },
          success: function(result) { $("#duration").text(result); }
          }
       });
    }
    

    The above is just an example of how you can approach this.

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

Sidebar

Related Questions

I have two text fields in my view. I did it using IB. My
I have two text fields in my view. I did it using IB. in
Using MVC3 Razor, I have a partial view with some text, two text boxes
I have two view controller classes in my application delegate. I can change from
I have two view controllers in a tabbar which can both edit data. Therefore,
I have two view controllers that allow changes to the Address Book. The first
I have two view controllers name RootViewController and SecondViewController. In the FirstViewController I have
Hypothetically I have two tables Employee and Locations. Additionaly I have a view viewEmpLocation
I have two UIImageView objects inside my view (both 320x480 one above the other).
I have two views within one .xib (one view for landscape, another for portrait).

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.