seaofclouds/tweet — Issue #190: Handle negative values
Author: jamespalmer
Created: 2011-11-11 01:49:45+00:00
State: open

It may be a good idea to handle negative values. I've seen instances where a tweet returns "about -20 seconds ago" -- mainly when dealing with trending topics.

```javascript
function relative_time(date) {
var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
var delta = parseInt((relative_to.getTime() - date) / 1000, 10);
var r = '';
if (delta < 1) {
r = 'just now';
} else if (delta < 60) {
r = 'about ' + delta + ' seconds ago';
} else if(delta < 120) {
r = 'about a minute ago';
} else if(delta < (45*60)) {
r = (parseInt(delta / 60, 10)).toString() + ' minutes ago';
} else if(delta < (2*60*60)) {
r = 'about an hour ago';
} else if(delta < (24*60*60)) {
r = '' + (parseInt(delta / 3600, 10)).toString() + ' hours ago';
} else if(delta < (48*60*60)) {
r = 'about a day ago';
} else {
r = 'about ' + (parseInt(delta / 86400, 10)).toString() + ' days ago';
}
return r;
}
```