use as show below
var time = time_algo(time_stamp); //here time_algo call the function
function time_algo(time_stamp){
var t= new Date(); //present
var time = new Date(time_stamp); //past
//show seconds ago
if(t.getDate() == time.getDate() && t.getHours() == time.getHours() && t.getMinutes() == time.getMinutes() && time.getSeconds()<60)
{
var time=parseInt(t.getSeconds()) - parseInt(time.getSeconds()) + " seconds ago";
return time;
}
//Minutes ago
else if(t.getDate() == time.getDate() && t.getHours() == time.getHours())
{
var time=parseInt(t.getMinutes()) - parseInt(time.getMinutes()) + " minutes ago";
return time;
}
// if today show hours ago
else if(t.getDate() == time.getDate()){
var y= parseInt(t.getHours()) - parseInt(time.getHours()) +" hours ago";
return y;
}
//Yesterday
else if(parseInt(t.getDate())-1 == parseInt(time.getDate()))
{
var time = "Yesterday at "+time.getHours()+":"+time.getMinutes();
return time;
}
//date, month at time : min
var monthNames = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"];
var time = time.getDate()+" "+ monthNames[time.getMonth()] +" at "+time.getHours()+":"+time.getMinutes();
return time;
}
var time = time_algo(time_stamp); //here time_algo call the function
function time_algo(time_stamp){
var t= new Date(); //present
var time = new Date(time_stamp); //past
//show seconds ago
if(t.getDate() == time.getDate() && t.getHours() == time.getHours() && t.getMinutes() == time.getMinutes() && time.getSeconds()<60)
{
var time=parseInt(t.getSeconds()) - parseInt(time.getSeconds()) + " seconds ago";
return time;
}
//Minutes ago
else if(t.getDate() == time.getDate() && t.getHours() == time.getHours())
{
var time=parseInt(t.getMinutes()) - parseInt(time.getMinutes()) + " minutes ago";
return time;
}
// if today show hours ago
else if(t.getDate() == time.getDate()){
var y= parseInt(t.getHours()) - parseInt(time.getHours()) +" hours ago";
return y;
}
//Yesterday
else if(parseInt(t.getDate())-1 == parseInt(time.getDate()))
{
var time = "Yesterday at "+time.getHours()+":"+time.getMinutes();
return time;
}
//date, month at time : min
var monthNames = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"];
var time = time.getDate()+" "+ monthNames[time.getMonth()] +" at "+time.getHours()+":"+time.getMinutes();
return time;
}
0 comments:
Post a Comment