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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:25:31+00:00 2026-05-26T08:25:31+00:00

I need to know how to do relative time in rails but not as

  • 0

I need to know how to do relative time in rails but not as a sentence, more like something i could do this with (when i input format like this 2008-08-05 23:48:04 -0400)

if time_ago < 1 hour, 3 weeks, 1 day, etc.
    execute me
end
  • 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-26T08:25:32+00:00Added an answer on May 26, 2026 at 8:25 am

    Basic relative time:

    # If "time_ago" is more than 1 hour past the current time
    if time_ago < 1.hour.ago
      execute_me
    end
    

    Comparison:

    Use a < to see if time_ago is older than 1.hour.ago, and > to see if time_ago is more recent than 1.hour.ago

    Combining times, and using fractional times:

    You can combine times, as davidb mentioned, and do:

    (1.day + 3.hours + 2500.seconds).ago
    

    You can also do fractional seconds, like:
    0.5.seconds.ago

    There is no .milliseconds.ago, so if you need millisecond precision, just break it out into a fractional second. That is, 1 millisecond ago is:

    0.001.seconds.ago
    

    .ago() in general:

    Putting .ago at the end of just about any number will treat the number as a #of seconds.

    You can even use fractions in paranthesis:

    (1/2.0).hour.ago   # half hour ago
    (1/4.0).year.ago   # quarter year ago
    

    NOTE: to use fractions, either the numerator or denominator needs to be a floating point number, otherwise Ruby will automatically cast the answer to an integer, and throw off your math.

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

Sidebar

Related Questions

Need to know this so that i could send DTMF and that is going
I need to know which iOS device is currently running app (saying more exactly
Ok, I know this sounds really daft to be asking here, but it is
This is more like a knowledge question than actual implementation. I was wondering if
Possible Duplicate: How do I calculate relative time? I want to format dates on
I know this may seem quite basic to geeks. But I want to make
Need to know which event triggered after text selection done in Apple iPad. It
I need to know how to change background of expanded list item when it
I need to know how to exclude files/folders from php generated zip. Here is
I need to know on a continuous basis when my UIScrollView is scrolling or

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.