Created
January 28, 2014 21:07
-
-
Save bsstoner/8676534 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var relativeDate = (function(undefined){ | |
var SECOND = 1000, | |
MINUTE = 60 * SECOND, | |
HOUR = 60 * MINUTE, | |
DAY = 24 * HOUR, | |
WEEK = 7 * DAY, | |
YEAR = DAY * 365, | |
MONTH = YEAR / 12; | |
var formats = [ | |
[ 0.7 * MINUTE, 'just now' ], | |
[ 1.5 * MINUTE, 'a minute ago' ], | |
[ 60 * MINUTE, 'minutes ago', MINUTE ], | |
[ 1.5 * HOUR, 'an hour ago' ], | |
[ DAY, 'hours ago', HOUR ], | |
[ 2 * DAY, 'yesterday' ], | |
[ 7 * DAY, 'days ago', DAY ], | |
[ 1.5 * WEEK, 'a week ago'], | |
[ MONTH, 'weeks ago', WEEK ], | |
[ 1.5 * MONTH, 'a month ago' ], | |
[ YEAR, 'months ago', MONTH ], | |
[ 1.5 * YEAR, 'a year ago' ], | |
[ Number.MAX_VALUE, 'years ago', YEAR ] | |
]; | |
function relativeDate(input,reference){ | |
!reference && ( reference = (new Date).getTime() ); | |
reference instanceof Date && ( reference = reference.getTime() ); | |
input instanceof Date && ( input = input.getTime() ); | |
var delta = reference - input, | |
format, i, len; | |
for(i = -1, len=formats.length; ++i < len; ){ | |
format = formats[i]; | |
if(delta < format[0]){ | |
return format[2] == undefined ? format[1] : Math.round(delta/format[2]) + ' ' + format[1]; | |
} | |
}; | |
} | |
return relativeDate; | |
})(); | |
if(typeof module != 'undefined' && module.exports){ | |
module.exports = relativeDate; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment