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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:24:59+00:00 2026-05-14T19:24:59+00:00

I”m looking to get this correct and i’m getting a bit fustrated with this.

  • 0

I”m looking to get this correct and i’m getting a bit fustrated with this. What I want to do is get hours and days and weeks correct.

Example:

if this post is < 60min old then have it read: Posted Less then 1 minute ago

if this post is < 120min old then have it read: Posted 1 hour ago

if this post is > 120min old then have it read: Posted 1 hours ago

if this post is < 1440min old then have it read: Posted 1 day ago

if this post is > 1440min old then have it read: Posted 2 days ago

Is that right??

This is what I have so far:

if (lapsedTime < 60) {
        return '< 1 mimute';
    } else if (lapsedTime < (60*60)) {
        return Math.round(lapsedTime / 60) + 'minutes';
    } else if (lapsedTime < (12*60*60)) {
        return Math.round(lapsedTime / 2400) + 'hr';
    } else if (lapsedTime < (24*60*60)) {
        return Math.round(lapsedTime / 3600) + 'hrs';
    } else if (lapsedTime < (7*24*60*60)) {
        return Math.round(lapsedTime / 86400) + 'days';
    } else {
        return Math.round(lapsedTime / 604800) + 'weeks';
    }
  • 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-14T19:25:00+00:00Added an answer on May 14, 2026 at 7:25 pm

    You had a few typos and missed cases:

        if (lapsedTime < 60) {
            return '< 1 minute';
        } else if (lapsedTime < (2*60*60)) {     // Missed this case
            return '1 minute';
        } else if (lapsedTime < (60*60)) {
            return Math.round(lapsedTime / 60) + ' minutes';
        } else if (lapsedTime < (2*60*60)) {     // This should be 2, not 12
            return '1 hour';
        } else if (lapsedTime < (24*60*60)) {
            return Math.round(lapsedTime / 3600) + ' hours';
        } else if (lapsedTime < (2*24*60*60)) {
            return '1 day';
        } else if (lapsedTime < (7*24*60*60)) {
            return Math.round(lapsedTime / 86400) + ' days';
        } else if (lapsedTime < (2*7*24*60*60)) {  // Missed this case
            return '1 week';
        } else {
            return Math.round(lapsedTime / 604800) + ' weeks';
        }
    

    I do agree that a better approach would be to calculate the weeks, days, hours, minutes, and use those to format a string:

    function formatTime(t, tStr) {
        // Singular case
        if(t==1) { return t+' '+tStr; }
    
        // Plural case
        return t+' '+tStr+'s';
    }
    
    function timeString(lapsedTime) {
        // These could be "round" or "floor", depending on what you want
        var minutes = Math.floor(lapsedTime/60);
        var hours = Math.floor(lapsedTime/3600);
        var days = Math.floor(lapsedTime/86400);
        var weeks = Math.floor(lapsedTime/604800);
        var years = Math.floor(lapsedTime/31536000);
    
        if(minutes == 0) {  return '< 1 minute';                    }
        if(hours == 0)   {  return formatTime(minutes, 'minute');   }
        if(days == 0)    {  return formatTime(hours, 'hour');       }
        if(weeks == 0)   {  return formatTime(days, 'day');         }
        if(years == 0)   {  return formatTime(weeks, 'week');       }
        return formatTime(years, 'year');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to change all headers, normally this is quite easy (and it probably
I'm looking for a way to convert text like this: <!DOCTYPE html PUBLIC \-//W3C//DTD
I'm working with in memory xml of daily stock market data, and I'm getting
I'm using Socket.IO on Node.js. The Example here: http://socket.io/#how-to-use First Example. I have tested
This is in Visual Studio 2010 IDE. I need to remove the quotes from
this is what i have right now Drawing an RSS feed into the php,
in this code I'm trying to Listening to multi connection at the same time
I don't get it. I have two activities in which i parse an XML-file
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I've put together this small little piece but I'm having problems with the renderings

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.