Saturday, 1 November 2014

Get Time Difference like Facebook Post 30 min ago 50 ago 1 hour ago or October 23

function getTimeString(givenDate) {

var monthNames = new Array();

monthNames[0] = "January";

monthNames[1] = "February";

monthNames[2] = "March";

monthNames[3] = "April";

monthNames[4] = "May";

monthNames[5] = "June";

monthNames[6] = "July";

monthNames[7] = "August";

monthNames[8] = "September";

monthNames[9] = "October";

monthNames[10] = "November";

monthNames[11] = "December";

var previousDate=new Date(givenDate);

var currentDate=new Date();

var milliSeconds=currentDate.getTime()-previousDate.getTime();

var daysDifference = Math.floor(milliSeconds/1000/60/60/24);

var timeDiffernceText="";




 
if( daysDifference ==0)



{

milliSeconds -= daysDifference*1000*60*60*24
 
var hoursDifference = Math.floor(milliSeconds/1000/60/60);

if(hoursDifference==0)



{

milliSeconds -= hoursDifference*1000*60*60
 
var minutesDifference = Math.floor(difference/1000/60);



timeDiffernceText=minutesDifference.toString();
 
timeDiffernceText=timeDiffernceText + " Minutes";



}
 
else



{

timeDiffernceText=hoursDifference.toString();
 
timeDiffernceText=timeDiffernceText+" Hours";



}

}
 
else



{
 
var month=previousDate.getMonth();



timeDiffernceText=monthNames[month];
 
timeDiffernceText=timeDiffernceText+" "+ previousDate.getDate().toString();



}
 
return timeDiffernceText;



}