まともに node.js でつかえる ago.js
なんか npm にアホみたいにたくさんあがっているが、just now の処理がおかしかったり、アホみたいに巨大だったり、まあそんなかんじなんで、ejresig の実装をもとにちょっと改変したやつをおいておきますね。
具体的には引数を Date にして、デフォルトの値を %Y-%m-%d にしてある。
/*
* Based on:
* JavaScript Pretty Date
* Copyright (c) 2011 John Resig (ejohn.org)
* Licensed under the MIT and GPL licenses.
*
* And modified by tokuhirom, license is same as above.
*/
module.exports = function prettyDate(date){
var diff = (((new Date()).getTime() - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
return date.toISOString().replace(/T.+/, '');
return day_diff == 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}
ライブラリは simple & readable でありたいものですね。
Published: 2012-09-09(Sun) 22:36