• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

RelativeTimeScripts

Page history last edited by PBworks 15 years, 11 months ago

On March 27th, the relative_created_at attribute in the API will be removed. To remedy this, some intrepid folks wrote up scripts to handle this functionality.

 

Javascript by Mike Demers

 

as used in Tweetbar

 


function relative_time(time_value) {
   var parsed_date = Date.parse(time_value);

   var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
   var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);

   if(delta < 60) {
       return 'less than a minute ago';
   } else if(delta < 120) {
       return 'about a minute ago';
   } else if(delta < (45*60)) {
       return (parseInt(delta / 60)).toString() + ' minutes ago';
   } else if(delta < (90*60)) {
           return 'about an hour ago';
       } else if(delta < (24*60*60)) {
       return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
   } else if(delta < (48*60*60)) {
       return '1 day ago';
   } else {
       return (parseInt(delta / 86400)).toString() + ' days ago';
   }
}

 


 

PHP by Till Kr├╝ss

 

as used in Tweets Pepper

 


// $time should be a Unix timestamp - get it with strtotime()
function get_elapsedtime($time) {

    $gap = time() - $time;

    if ($gap < 5) {
        return 'less than 5 seconds ago';
    } else if ($gap < 10) {
        return 'less than 10 seconds ago';
    } else if ($gap < 20) {
        return 'less than 20 seconds ago';
    } else if ($gap < 40) {
        return 'half a minute ago';
    } else if ($gap < 60) {
        return 'less than a minute ago';
    }

    $gap = round($gap / 60);
    if ($gap < 60)  { 
        return $gap.' minute'.($gap > 1 ? 's' : '').' ago';
    }

    $gap = round($gap / 60);
    if ($gap < 24)  { 
        return 'about '.$gap.' hour'.($gap > 1 ? 's' : '').' ago';
    }

    return date('h:i A F d, Y', $time);

}

 


 

 

Perl by Rick Measham (twitter: RickMeasham)

 


# This code uses DateTime as that's what I use.
use DateTime::Format::HTTP;

sub relative_time {
    my $time_value = shift;
    my $relative_to = shift || DateTime->now()->epoch;
    $relative_to = $relative_to->epoch if ref($relative_to) =~ /DateTime/;

    my $parsed_date =
DateTime::Format::HTTP->parse_datetime($time_value)->epoch;
 
    my $delta = $relative_to - $parsed_date;

    if($delta < 60) {
        return 'less than a minute ago';
    } elsif($delta < 120) {
        return 'about a minute ago';
    } elsif($delta < (45*60)) {
        return int($delta / 60) . ' minutes ago';
    } elsif($delta < (90*60)) {
        return 'about an hour ago';
    } elsif($delta < (24*60*60)) {
        return 'about ' . int($delta / 3600) . '
hours ago';
    } elsif($delta < (48*60*60)) {
        return '1 day ago';
    } else {
        return int($delta / 86400) . ' days ago';
    }
}

 


 

Python by Mike Taylor (aka bear)

 

yes, I know the doctest part is longer than the code. But that's a good thing :)

 


from time import strptime, mktime, localtime

_partialMinute = 45 * 60
_partialHour   = 90 * 60
_fullDay       = 24 * 60 * 60
_twoDays       = _fullDay * 2

def relative_time(dateString, currentTime=None):
    """
    Take the created_at timestamp string and convert
    it to a relative text expression
    
    >>> now = mktime((2007, 3, 20, 11, 00, 00, 1, 80, 1))
    >>> relative_time('Tue Mar 20 10:59:30 +0000 2007', currentTime=now)
    'less than a minute ago'
    
    >>> relative_time('Tue Mar 20 10:58:30 +0000 2007', currentTime=now)
    'about a minute ago'
    
    >>> relative_time('Tue Mar 20 10:30:00 +0000 2007', currentTime=now)
    '30 minutes ago'
    
    >>> relative_time('Tue Mar 20 10:00:30 +0000 2007', currentTime=now)
    'about an hour ago'
    
    >>> relative_time('Tue Mar 20 08:00:00 +0000 2007', currentTime=now)
    'about 3 hours ago'
    
    >>> relative_time('Wed Mar 19 10:00:00 +0000 2007', currentTime=now)
    '1 day ago'
    
    >>> relative_time('Wed Mar 18 10:00:00 +0000 2007', currentTime=now)
    '2 days ago'
    """
    if currentTime is not None:
        now = currentTime
    else:
        now = mktime(localtime())

    date   = dateString.replace(' +0000', '')
    parsed = mktime(strptime(date, '%a %b %d %H:%M:%S %Y'))
    delta  = now - parsed

    if delta < 60:
        return 'less than a minute ago'
    elif delta < 120:
        return 'about a minute ago'
    elif delta < _partialMinute:
        return '%d minutes ago' % (delta / 60)
    elif delta < _partialHour:
        return 'about an hour ago'
    elif delta < _fullDay:
        return 'about %d hours ago' % (delta / 3600)
    elif delta < _twoDays:
        return '1 day ago'
    else:
        return '%d days ago' % (delta / 86400)

if __name__ == "__main__":
    import doctest
    doctest.testmod()

 


 

Flash Actionscript by Jesse Edwards

 


/**
 * getDateFromString()
 * @param str String - date in Thu Jul 8 12:48:23 GMT+0800 2004 format
 * @return date - date in flash format
 */
function getDateFromString(str:String):Date
{
        var m   = {Jan:0, Feb:1, Mar:2, Apr:3, May:4, Jun:5, Jul:6, Aug:7, Sep:8, Oct:9, Nov:10, Dec:11};
        var dArr:Array  = str.split(' ');
        var tArr:Array  = dArr[3].split(':');

        return new Date(Date.UTC(dArr[5], m[dArr[1]], dArr[2], tArr[0], tArr[1], tArr[2]));

}
/**
 * relative_time()
 * @param time_value - date in Thu Jul 8 12:48:23 GMT+0800 2004 format
 * @return string
 */
function relative_time(time_value):String
{
        var parsed_date:Date    = getDateFromString(time_value);
        var relative_to:Date    = new Date();
        var dateDiff:Number     = (relative_to.getTime() - parsed_date.getTime()) / 1000;

        switch(true){
                case dateDiff < 60:
                        return 'about a minute ago';
                        break;
                case dateDiff < 120:
                        return 'about a minute ago';
                        break;
                case dateDiff < (45*60):
                        return int(dateDiff / 60) + ' minutes ago';
                        break;
                case dateDiff < (90*60):
                        return 'about an hour ago';
                        break;
                case dateDiff < (24*60*60):
                        return 'about ' + int(dateDiff / 3600) + ' hours ago';
                        break;
                case dateDiff < (48*60*60):
                        return '1 day ago';
                        break;
                default:
                        return int(dateDiff / 86400) + ' days ago';
                        break;
        }
}

 


 

C# by Weiran Zhang

 

as used in Twitterlicious

 


private String GetRelativeDate(DateTime date)
{
    DateTime now = DateTime.Now;
    TimeSpan span = now - date;
    if (span <= TimeSpan.FromSeconds(60))
    {
        return span.Seconds + " seconds ago";
    }
    else if (span <= TimeSpan.FromMinutes(60))
    {
        if (span.Minutes > 1)
        {
            return "about " + span.Minutes + " minutes ago";
        }
        else
        {
            return "about a minute ago";
        }
    }
    else if (span <= TimeSpan.FromHours(24))
    {
        if (span.Hours > 1)
        {
            return "about " + span.Hours + " hours ago";
        }
        else
        {
            return "about an hour ago";
        }
    }
    else
    {
        if (span.Days > 1)
        {
            return "about " + span.Days + "days ago";
        }
        else
        {
            return "about a day ago";
        }
    }
}

 

ColdFusion by Raymond Camden

 

This is a slightly modified version of the JavaScript code.

 


function relativeTime(pastdate) {
	var delta = dateDiff("s", pastDate, now());

	if(delta < 60) {
	    return "less than a minute ago";
	} else if(delta < 120) {
	    return "about a minute ago";
	} else if(delta < (45*60)) {
	    return round(delta/60) & " minutes ago";
	} else if(delta < (90*60)) {
	    return "about an hour ago";
	} else if(delta < (24*60*60)) {
		return round(delta/3600) & " hours ago";
	} else if(delta < (48*60*60)) {
	    return "1 day ago";
	} else {
		return round(delta/86400) & " days ago";
	}
}

Comments (1)

oyun said

at 2:55 am on May 31, 2010

http://www.kucukpatisserie.com/
http://www.gunalsen.com
http://www.kelebekcafe.net
http://www.kelebekcafe.org
http://www.mirclider.com
http://www.mirclider.net
http://www.mirclider.org
http://www.doruknet.org
http://www.dorukchat.net
http://www.dorukchat.org
http://www.netzurna.com
http://www.onursan.org
http://www.gevezechat.org
http://www.izmirchat.org
http://www.cinselchat.org
http://www.hanimeligelinlik.com
http://www.hanimeligelinlik.net
http://www.hanimeligelinlik.org
http://www.oyunlider.com
http://sikisyeri.blogspot.com/
http://fikra34.blogspot.com/
http://gunluk-filim.blogspot.com/
http://cinsel-muhabbet.blogspot.com
http://escinselsohbet.blogspot.com
http://cinselsohbetiniz.blogspot.com
http://gaysohbetiniz.blogspot.com
http://lezsohbetiniz.blogspot.com
http://ist-lgbtt.blogspot.com/
http://travesti34.blogspot.com/
http://gay34.blogspot.com/
http://gay-lez.blogspot.com/
http://gay-europa.blogspot.com/
http://drkcht.blogspot.com/
http://sarki-s0zleri.blogspot.com/
http://www.facebook.com/OnurSan.Fan
http://www.facebook.com/GunalSen
http://www.facebook.com/Partneriniz
http://www.facebook.com/AdultChat
http://twitter.com/OnurSanFan
http://twitter.com/DiningServices
http://twitter.com/WSUCougarscom
http://twitter.com/WSUCVMrecruit
http://cinselsohbet.blog.com/
http://gaychat.blog.com/
http://gaysohbet.blog.com/
http://sicaksohbet.blog.com/
http://sicakchat.blog.com/
http://sexchat.blog.com/
http://sexsohbet.blog.com/
http://diziizley1n.blog.com/
http://diziizleyin1.blog.com/
http://fikrabul.blog.com/
http://sikisturk.blog.com/
http://amcik.blog.com/
http://travesti.blog.com/
http://mircindir.blog.com/
http://mircyukle.blog.com/
http://kelebekindir.blog.com/
http://kelebekchat.blog.com/
http://kelebeksohbett.blog.com/
http://kelebektr.blog.com/
http://kelebekturk.blog.com/
http://trsohbet.blog.com/
http://chatnet.blog.com/

You don't have permission to comment on this page.