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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:32:04+00:00 2026-05-20T17:32:04+00:00

I have the following custom function for converting hours into unit of time: def

  • 0

I have the following custom function for converting hours into unit of time:

  def time_expiry_text(time_expiry) # unit of time is hours
    time_expiry_text = ''

    if ( (time_expiry / 24) > 30 ) then # if more than 30 days use months as unit
      months = ( (time_expiry / 24) / 30 )
      time_expiry_text = months.to_s
      time_expiry_text += months == 1 ? ' month' : ' months'
    elsif time_expiry >= 24 then # if greater than or equal to 1 day or 24 hours use days as unit
      days = time_expiry / 24
      time_expiry_text = days.to_s
      time_expiry_text += days == 1 ? ' day' : ' days'
    else
      hours = time_expiry
      time_expiry_text = hours.to_s
      time_expiry_text += hours == 1 ? ' hour' : ' hours'
    end

    return time_expiry_text
  end

I am having two issues:

  1. Sometimes I don’t always get the result I want. For ex, I got a return time of -2700 hours.

  2. When the unit of time is months, I also want to return the days left as well. For example, 2 months and 13 days.

  • 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-20T17:32:04+00:00Added an answer on May 20, 2026 at 5:32 pm

    Assuming you’re using a broad sense of the definition of a month (which isn’t always 30 days, but it seems like your logic here isn’t date-related)

    hours = 16530
    => 16530    
    days = hours / 24 
    => 688 #days
    extra_hours = hours % 24
    => 18 #hours (unallocated)
    months = days/30 
    => 22 #months
    extra_days = days % 30
    => 28 #days (unallocated)
    
    string = ""
    
    string += months.to_s + " months " if months 
    string += extra_days.to_s + " days " if extra_days
    string += extra_hours.to_S + " hours " if extra_hours
    string.strip! #or string = string.strip
    
    string
    => "22 months 28 days 18 hours"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom control that has the following prototype. Type.registerNamespace('Demo'); Demo.CustomTextBox = function(element)
I have the following custom function in ~/.emacs: (defun xi-rgrep (term) (grep-compute-defaults) (interactive sSearch
I have the following code in WCF service to throw a custom fault based
I have a custom sharepoint web part that is throwing the following exception: System.Security.SecurityException:
The string passed to my custom function is the following: SELECT key FROM ubis
Say I have the following custom component: <s:Group xmlns:fx=http://ns.adobe.com/mxml/2009 xmlns:s=library://ns.adobe.com/flex/spark xmlns:mx=library://ns.adobe.com/flex/mx> <fx:Script> <![CDATA[ [Bindable]
I have the following lines of jQuery: // When dragging ends stop: function(event, ui)
I have a custom function which returns either 0 or 1 depending on two
I have a custom EL function defined as follows: <function> ... <function-signature> java.lang.String getAsText(com.test.Outerclass.InnerEnum)
I have following situation: I have loged user, standard authentication with DB table $authAdapter

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.