JavaScript: Minutes / Hours Ago from String

Using JavaScript, I would like to know how many minutes or hours ago a time and date are, if the time is less than 24 hours old.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">


function resetTime(d) {
    d = new Date(d);
    d.setHours(0);
    d.setMinutes(0);
    d.setSeconds(0);
    d.setMilliseconds(0);
    return d;
}


function formatDateTime(strDate,strCurrent) {
    var dS = resetTime(strDate);
    var dC = resetTime(strCurrent);
    var dt = (dC-dS)/(24*3600*1000); 
    var arr = ["Today", "Yesterday", "Two Days Ago", "Three Days Ago"];
    
    return (dt>=0&&dt<4)?arr[dt]:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","December"][dS.getMonth(dS)] + " " + dS.getDate() + ", " + dS.getFullYear();
}

alert( formatDateTime('9/2/2013 3:00:01 PM',  '9/2/2013 3:00:07 PM' )); // Just Posted
alert( formatDateTime('9/2/2013 2:59:07 AM',  '9/2/2013 3:00:07 PM' )); // 1 Minute Ago
alert( formatDateTime('9/2/2013 2:12:07 AM',  '9/2/2013 3:00:07 PM' )); // 48 Minutes Ago
alert( formatDateTime('9/2/2013 2:00:01 PM',  '9/2/2013 3:00:07 PM' )); // 1 Hour Ago
alert( formatDateTime('9/2/2013 1:00:01 AM',  '9/2/2013 3:00:07 PM' )); // 14 Hours Ago
alert( formatDateTime('9/1/2013 11:00:07 PM', '9/2/2013 3:00:07 PM' )); // Yesterday
alert( formatDateTime('8/31/2013 11:00:07 PM', '9/2/2013 3:00:07 PM' )); // Two Days Ago
alert( formatDateTime('8/30/2013 11:00:07 PM', '9/2/2013 3:00:07 PM' )); // Three Days Ago
alert( formatDateTime('8/29/2013 11:00:07 PM', '9/2/2013 3:00:07 PM' )); // Aug 29, 2013


</script>
</head>
<body>
</body>
</html>

Open in new window

LVL 16
hankknightAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
check this : http://momentjs.com/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
function Diff(date1, date2)
{
	var date1 = new Date(date1);
	var date2 = new Date(date2);
	var seconds = (date2.getTime() - date1.getTime()) / 1000;
	days = parseInt(seconds / 86400);
	hours = parseInt((seconds - (days * 86400)) / 3600);
	minutes = parseInt((seconds - (days * 86400) - (hours * 3600)) / 60);
	secs = seconds - (days * 86400) - (hours * 3600) - (minutes * 60);
	alert(days + ":" + hours + ":" + minutes +":" + secs);
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.