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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:57:29+00:00 2026-06-17T16:57:29+00:00

I am trying to define a new getter for the date object. However it

  • 0

I am trying to define a new getter for the date object. However it returns false. Where’s my error?

Date.prototype.__defineGetter__('ago', function(){
    var diff = ((( new Date()).getTime() - this.getTime()) / 1000)
    , day_diff = Math.floor(diff / 86400);
    return day_diff == 0 && (
    diff < 60 && "just now" ||
    diff < 120 && "1 minute ago" ||
    diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
    diff < 7200 && "1 hour ago" ||
    diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
    day_diff == 1 && "Yesterday" ||
    day_diff < 7 && day_diff + " days ago" ||
    day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
});

var a = new Date('12/12/1990');
console.log(a.ago);
  • 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-17T16:57:30+00:00Added an answer on June 17, 2026 at 4:57 pm

    You didn’t make a test for more than one month ago so it just returns false, which is the last value from your || operations.

    [...]
    day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago" ||
    'More than a month ago';
    [...]
    
    var a = new Date('12/12/1990');
    console.log(a.ago()); //More than a month ago
    

    Fiddle

    Also, __defineGetter__ is non-standard and deprecated so I’ve replaced it by a standard prototype method property in the fiddle. Here’s the fiddle with the original getter.

    edit: ES5 provides a standard Object.defineProperty method, see @bfavaretto and @Bergi’s versions in the comments below:

    Object.defineProperty(Date.prototype, "ago", {
        get: function() {
            [...]
        }
    });
    

    The prototype method seems slightly faster than defineProperty, however taking in consideration the error margin with results varying from 60 to 230 million OPs/sec in the latest stable Chrome’s V8, there isn’t a noticeable performance difference. Property lookups are extremely fast so there shouldn’t be any notable difference even in a Node environment.

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

Sidebar

Related Questions

I'm trying to define a function for the NodeList object. This is the code:
I'm trying to define a new array through a where query but I can
I'm trying to define a new shortcode in WordPress, and I get the following
I'm trying to define a new type and have not had much luck finding
I'm trying to define a new URL handler under OSX that will point at
I am trying to define a dynamic var in a different namespace. The Lobos
I'm trying to define a very simple function in Drools as below: import java.util.List;
I'm new to Objective C. I am trying to define Constants which have behavior
I am new to Haskell, and programming in general. I am trying to define
New to Perl, I'm trying to define a subset of characters and then output

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.