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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:09:41+00:00 2026-05-22T03:09:41+00:00

given a timestamp, I could like to have a method that returns the correct

  • 0

given a timestamp, I could like to have a method that returns the correct unit (secs, hours, minutes) and the correct metric for that unit.

Examples, for a timestamp:

1 sec ago - Unit: SEC, Metric: 1
20 secs ago - Unit: SECS, Metric: 20
1 hour ago - Unit: HOUR, Metric: 1
3 hour ago - Unit: HOURS, Metric: 3
25 hours ago - Unit: DAY, Metric: 1
50 hours ago - Unit: DAYS, Metric: 2

For this method I only need to go support, sec, hours and days. Not months. Ideas?

  • 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-22T03:09:42+00:00Added an answer on May 22, 2026 at 3:09 am

    Most of the logic is already as a helper in Rails: http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words

    Something like this maybe:

    def words_and_units(from_time, to_time = 0)

    from_time = from_time.to_time if from_time.respond_to?(:to_time)
    to_time = to_time.to_time if to_time.respond_to?(:to_time)
    distance_in_minutes = (((to_time – from_time).abs)/60).round
    distance_in_seconds = ((to_time – from_time).abs).round

    case distance_in_minutes
      when 0..1
    
        case distance_in_seconds
          when 0..59   then {:metric => distance_in_seconds, :unit => 'SEC'}
          else             {:metric => 1, :unit => 'MIN'}
        end
    
      when 2..44           then {:metric => distance_in_minutes, :unit => 'MIN'}
      when 45..89          then {:metric => 1, :unit => 'HOUR'}
      when 90..1439        then {:metric => (distance_in_minutes.to_f / 60.0).round, :unit => 'HOUR'}
      when 1440..2529      then {:metric => 1, :unit => 'DAY'}
      when 2530..43199     then {:metric => (distance_in_minutes.to_f / 1440.0).round, :unit => 'DAY'}
      else
        raise WayToLongError
    end
    

    end

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

Sidebar

Related Questions

Given that Date has a method called after(Date) and Timestamp has a method the
Given <xsl:variable name=datePrecision as=element()*> <p>Year</p> <p>Month</p> <p>Day</p> <p>Time</p> <p>Timestamp</p> </xsl:variable> The expression $datePrecision[5] returns
Given a specific DateTime value, how do I display relative time, like: 2 hours
I have a table that looks like this CREATE TABLE `time_table` ( `id` INT(10)
In Java, given a timestamp, how to reset the time part alone to 00:00:00
Given an Oracle table created using the following: CREATE TABLE Log(WhenAdded TIMESTAMP(6) WITH TIME
Given a data stream of continuously arriving items containing a timestamp and text (e.g.
what is exactly happing when creating a Unix Timestamp from a given date. I
I have a query of the following form: SELECT * FROM MyTable WHERE Timestamp
i have some SQL code that is inserting values from another (non sql-based) system.

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.