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

  • Home
  • SEARCH
  • 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 7884803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:53:58+00:00 2026-06-03T04:53:58+00:00

Is there a method in ruby/rails for adding/subtracting date and time along with timezone

  • 0

Is there a method in ruby/rails for adding/subtracting date and time along with timezone and the output to be as count of days, hours, minutes and seconds instead of DataTime format?

t1 = "2012-05-05 00:00:02 -0400"
t2 = "2012-05-04 00:00:00 -0500"

time_diff = (Time.now() - Time.parse(t1)).to_s
#Or
time_diff = (Time.parse(t1) - Time.parse(t2)).to_s

I am looking for 1 Day, 01:59:58
The result should #of Days HH:MM:SS format

  • 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-06-03T04:54:00+00:00Added an answer on June 3, 2026 at 4:54 am

    As far as I know there’s nothing built-in to Ruby or Rails to do this. There is distance_of_time_in_words, but it gives a fuzzier time than you seem to be looking for (e.g. “about 7 days”).

    It’s fairly trivial to write a method to do this, though. Given a difference in seconds, this will give you the days, hours, and seconds in the difference:

    def time_length seconds
      days = (seconds / 1.day).floor
      seconds -= days.days
      hours = (seconds / 1.hour).floor
      seconds -= hours.hours
      minutes = (seconds / 1.minute).floor
      seconds -= minutes.minutes
      { days: days, hours: hours, minutes: minutes, seconds: seconds }
    end
    
    time_length(Time.parse(t1) - Time.parse(t2))
    #=> {:days=>0, :hours=>23, :minutes=>0, :seconds=>2.0}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think there is a method within ruby or rails that does this but
Is there any method available to calculate percentage in rails or ruby ?? EDIT:
In Ruby is there any method that lists all the global variables available at
Is there a pretty way to make a series of method calls in ruby
Is there a well-designed method that can run my ruby program from anywhere? I
This is a helper method I found in the ruby on rails application spree
In Ruby on Rails, each model entity has a validate_on_ something hook method, that
So I am creating a time tracking application using Ruby On Rails and am
I'm using Ruby on Rails and i have a find_or_create_by_custom_stuff method. However i would
I have a short question on ruby / rails method naming conventions or good

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.