Skip to content

Instantly share code, notes, and snippets.

@ahomu
Created May 24, 2013 12:00
Show Gist options
  • Save ahomu/5643014 to your computer and use it in GitHub Desktop.
Save ahomu/5643014 to your computer and use it in GitHub Desktop.
x日前的な
'use strict';
/*
* JavaScript Pretty Date
* Copyright (c) 2011 John Resig (ejohn.org)
* Licensed under the MIT and GPL licenses.
*
* @original http://ejohn.org/blog/javascript-pretty-date/
*/
Util || (Util = {});
Util.date = (function() {
function format(mstime) {
var date = new Date(mstime);
return date.getMonth()+1 + '月 ' + date.getDate() + '日';
}
return {
pretty: function(time) {
var mstime = Date.parse(time),
diff = ((Date.now() - mstime) / 1000),
day_diff = Math.floor(diff / 86400);
if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) {
return format(mstime);
}
return day_diff === 0 && (
diff < 60 && Math.floor(diff) + '秒前' ||
diff < 120 && '1分前' ||
diff < 3600 && Math.floor(diff / 60) + '分前' ||
diff < 7200 && '1時間前' ||
diff < 86400 && Math.floor(diff / 3600) + '時間前'
) ||
day_diff === 1 && '昨日' ||
day_diff < 7 && day_diff + '日前' ||
day_diff < 31 && Math.ceil(day_diff / 7) + '週間前';
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment