Created
April 17, 2013 09:05
-
-
Save ophentis/5402883 to your computer and use it in GitHub Desktop.
javascript timeago function in chinese and some modification
This file contains hidden or 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
function(time, local){ | |
(!local) && (local = Date.now()); | |
if (typeof time !== 'number' || typeof local !== 'number') return; | |
var offset = Math.abs((local/1000 - time)), | |
span = [], | |
MINUTE = 60, | |
HOUR = 3600, | |
DAY = 86400, | |
WEEK = 604800, | |
MONTH = 2629744, | |
YEAR = 31556926, | |
DECADE = 315569260; | |
if (offset <= MINUTE) span = [ '', '不久' ]; | |
else if (offset < HOUR) span = [ Math.floor(Math.abs(offset / MINUTE)), '分鐘' ]; | |
else if (offset < DAY) span = [ Math.floor(Math.abs(offset / HOUR)), '小時' ]; | |
else if (offset < WEEK) span = [ Math.floor(Math.abs(offset / DAY)), '日' ]; | |
else if (offset < YEAR) span = [ Math.floor(Math.abs(offset / WEEK)), '週' ]; | |
else if (offset < DECADE) span = [ Math.floor(Math.abs(offset / YEAR)), '年' ]; | |
else if (offset < (DECADE * 100)) span = [ Math.floor(Math.abs(offset / DECADE)), '百年' ]; | |
else span = [ '', '好久好久' ]; | |
// span[1] += (span[0] === 0 || span[0] > 1) ? '' : ''; | |
span = span.join(''); | |
return (time <= local) ? span + '之前' : '再' + span; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment