· 7 years ago · Jul 24, 2018, 06:14 AM
1/*
2 * JavaScript Pretty Date
3 * Copyright (c) 2008 John Resig (jquery.com)
4 * Licensed under the MIT license.
5 */
6
7// Takes an ISO time and returns a string representing how
8// long ago the date represents.
9function prettyDate(time){
10 var date = new Date(time); // (time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
11 diff = (((new Date()).getTime() - date.getTime()) / 1000),
12 day_diff = Math.floor(diff / 86400);
13
14 if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
15 return;
16
17 return day_diff == 0 && (
18 diff < 60 && "just now" ||
19 diff < 120 && "1 minute ago" ||
20 diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
21 diff < 7200 && "1 hour ago" ||
22 diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
23 day_diff == 1 && "Yesterday" ||
24 day_diff < 7 && day_diff + " days ago" ||
25 day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
26}
27
28// If jQuery is included in the page, adds a jQuery plugin to handle it as well
29if ( typeof jQuery != "undefined" )
30 jQuery.fn.prettyDate = function(){
31 return this.each(function(){
32 var date = prettyDate(this.title);
33 if ( date )
34 jQuery(this).text( date );
35 });
36 };