To calculate the elapsed time between now and the post's posting time, subtract the current time from the posting time in milliseconds. Use this remaining time to create a user-friendly '... ago' string.
For various JavaScript time functions, check out this link. Here is an algorithm I stumbled upon (in PHP):
function TimeAgo($datefrom,$dateto=-1)
{
// Set default values; treat 0 as error
if($datefrom<=0) {
return "A long time ago";
}
if($dateto==-1) {
$dateto = time();
}
// Calculate time difference in seconds
$difference = $dateto - $datefrom;
// Choose appropriate time interval based on difference
if($difference < 60)
{
$interval = "s";
}
elseif($difference >= 60 && $difference<60*60)
{
$interval = "n";
}
elseif($difference >= 60*60 && $difference<60*60*24)
{
$interval = "h";
}
...
// Include more cases for different intervals
switch($interval)
{
case "m":
// Logic for month interval
break;
case "y":
// Logic for year interval
break;
...
// More interval cases
}
return $res;
}