/ Published in: JavaScript
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function normalizeDate(time) { var values = time.split(" "); time = values[1] + " " + values[2] + ", " + values[5] + " " + values[3]; var parsed_date = Date.parse(time); 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 out = ''; if (delta < 60) { out = 'a minute ago'; } else if(delta < 120) { out = 'couple of minutes ago'; } else if(delta < (45*60)) { out = (parseInt(delta / 60)).toString() + ' minutes ago'; } else if(delta < (90*60)) { out = 'an hour ago'; } else if(delta < (24*60*60)) { out = '' + (parseInt(delta / 3600)).toString() + ' hours ago'; } else if(delta < (48*60*60)) { out = '1 day ago'; } else { out = (parseInt(delta / 86400)).toString() + ' days ago'; } return out; }