Comments on: Recursive “pretty date” https://j11y.io/snippets/recursive-pretty-date/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: Matt https://j11y.io/snippets/recursive-pretty-date/#comment-1151 Tue, 21 Jul 2009 14:43:50 +0000 https://j11y.io/?p=1022#comment-1151 Hi James,

Can u explain the pros and cons of writing your code like you wrote and like this:

function niceTime(time) {
 
    var ints = {
        second: 1,
        minute: 60,
        hour: 3600,
        day: 86400,
        week: 604800,
        month: 2592000,
        year: 31536000
    };
 
    time = +new Date(time);
 
    var gap = ((+new Date()) - time) / 1000,
        amount, measure;
 
    for (var i in ints) {
        if (gap > ints[i]) { measure = i; }
    }
 
    amount = gap / ints[measure];
    amount = gap > ints.day ? (Math.round(amount * 100) / 100) : Math.round(amount);
    amount += ' ' + measure + (amount > 1 ? 's' : '') + ' ago';
 
    return amount; 
};
]]>
By: David https://j11y.io/snippets/recursive-pretty-date/#comment-1150 Tue, 21 Jul 2009 09:33:08 +0000 https://j11y.io/?p=1022#comment-1150 @James: Doh, sorry! I made the stupid mistake in the code of assuming that aTime is less than bTime. Inserting this line after assignment of aTime and bTime solves the problem:

if (aTime > bTime) return arguments.callee(bTime,aTime);
]]>
By: James https://j11y.io/snippets/recursive-pretty-date/#comment-1149 Tue, 21 Jul 2009 08:49:05 +0000 https://j11y.io/?p=1022#comment-1149 Nice work David! I always forget about the native Date methods… Some of them are actually quite useful. I can’t quite get your solution to work though; it seems to work with past dates but, for example, I can’t get it to work with:

prettyAccurateTimeDiff( "January 1, 3000" );

@Ricky, yup, it’s deprecated; I have no idea why though, there really is no better alternative unless you want to use a named function within the closure and then return it by its identifier. I agree with Ben though; I doubt it’ll be released from implementations because so much code on the web depends on it.

]]>
By: David https://j11y.io/snippets/recursive-pretty-date/#comment-1148 Tue, 21 Jul 2009 08:26:55 +0000 https://j11y.io/?p=1022#comment-1148 Doh, return statement could be simplified to this, which would also eliminate some unnecessary recursion:

return (amount ? amount + " " + diffnames[diff] + (amount>1?"s":"") : "") + 
       (+bTime - +iTime > 1000 && diff+1 < diffs.length ? ", "+arguments.callee(iTime,bTime,diff+1) : "");

Still with messed-up special characters, sorry. 😛

]]>
By: David https://j11y.io/snippets/recursive-pretty-date/#comment-1147 Mon, 20 Jul 2009 19:33:14 +0000 https://j11y.io/?p=1022#comment-1147 @James

We can use the date objects’ get/set methods to avoid having to define year and month in seconds. That way, we get pretty and accurate!

var prettyAccurateTimeDiff = (function() {
 
  var diffnames = ["decade","year","month","week","day","hour","minute","second"],
      diffs = [
        function(d){ return d.setFullYear(d.getFullYear()+10);},
        function(d){ return d.setFullYear(d.getFullYear()+1);},
        function(d){ return d.setMonth(d.getMonth()+1);},
        function(d){ return d.setDate(d.getDate()+7);},
        function(d){ return d.setDate(d.getDate()+1);},
        function(d){ return d.setHours(d.getHours()+1);},
        function(d){ return d.setSeconds(d.getSeconds()+60);},
        function(d){ return d.setSeconds(d.getSeconds()+1);}
      ];
 
  return function (aTime, bTime, diff) {
    aTime = new Date(aTime);
    bTime = bTime === undefined ? new Date() : new Date(bTime);
    diff = diff || 0;
    var amount = 0, iTime = aTime;
    while(bTime>=diffs[diff](new Date(iTime))){
      iTime = diffs[diff](new Date(iTime));
      amount++;
    }
    return (amount ? amount + " " + diffnames[diff] + (amount>1?"s":"") + 
             (+bTime - +iTime > 1000 ? ", " : "") : "") + 
           (diff+1<diffs.length ? arguments.callee(iTime,bTime,diff+1): "");
    };
 
})();

Note: couldn’t figure out how to prevent munging of lesser-than and greater -than characters.

]]>
By: Ben Nadel https://j11y.io/snippets/recursive-pretty-date/#comment-1146 Mon, 20 Jul 2009 11:59:31 +0000 https://j11y.io/?p=1022#comment-1146 @Ricky,

I also saw that this was something deprecated. I don’t know why; I don’t think there is any other way to do this without knowing the name of the function, which, when anonymous, is not possible. I would think it is not something that will ever actually be removed from language support.

]]>
By: Ricky https://j11y.io/snippets/recursive-pretty-date/#comment-1145 Mon, 20 Jul 2009 02:36:20 +0000 https://j11y.io/?p=1022#comment-1145 Hi James, I wonder, does the arguments.callee( +new Date() – remainder*1000 ); is the recursive part in your code?
I tried to google for arguments.callee, but some references I found tells that this property has been deprecated.
Do you plan to implement another recursive way to replace it?
Thanks

]]>
By: Valentino https://j11y.io/snippets/recursive-pretty-date/#comment-1144 Sun, 19 Jul 2009 16:49:28 +0000 https://j11y.io/?p=1022#comment-1144 nice! I didn’t even know existed a “unary plus operator”! 😀

]]>
By: James https://j11y.io/snippets/recursive-pretty-date/#comment-1143 Sun, 19 Jul 2009 16:33:08 +0000 https://j11y.io/?p=1022#comment-1143 @josh, Unfortunately, the order of JavaScript hashes cannot be relied upon; you’ll get different results across different implementations. Another technique I considered was to have two arrays; one containing the time units (“second”, “minute” etc.) and the other containing their respective values (1,60,3600 etc.)…

]]>
By: Pascal https://j11y.io/snippets/recursive-pretty-date/#comment-1142 Sun, 19 Jul 2009 16:31:45 +0000 https://j11y.io/?p=1022#comment-1142 Brilliant timing [sic]: I was just looking for a way to showcase a count down to my baby’s birthday on his site ––never too early to manage fans ; )

Also my excuse to leave a comment and say how much I’ve been enjoying/learning from your site, James.

Cheers,

]]>