<script>
function relative_time(time_value) {
var values = time_value.split(" ");
time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
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);
delta = delta + (relative_to.getTimezoneOffset() * 60);
var r = '';
if (delta < 60) {
r = 'a minute ago';
} else if(delta < 120) {
r = 'couple of minutes ago';
} else if(delta < (45*60)) {
r = (parseInt(delta / 60)).toString() + ' minutes ago';
} else if(delta < (90*60)) {
r = 'an hour ago';
} else if(delta < (24*60*60)) {
r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
} else if(delta < (48*60*60)) {
r = '1 day ago';
} else if (delta < (30*24*60*60)) {
r = (parseInt(delta / 86400)).toString() + ' days ago';
} else if (delta < (60*24*60*60)) {
r = '1 month ago';
} else if (delta < (365*24*60*60)) {
r = (parseInt(delta / 30/86400)).toString() + ' months ago';
} else if (delta < (2*365*24*60*60)) {
r = '1 year ago';
} else {
r = (parseInt(delta / 365/86400)).toString() + ' years ago';
}
return r;
}
alert(relative_time('Sun Feb 25 16:02:22 +0000 2007'));
String.prototype.linkify = function() {
return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
return m.link(m);
});
};
alert('Link to http://google.com.vn'.linkify());
</script>
Title:
Javascript get relative_time and linkify function
Description:
<script> function relative_time(time_value) { var values = time_value.split(" "); time_value = values[1] + " ...
...
Rating:
4