Skip to content

Instantly share code, notes, and snippets.

@599316527
Created August 24, 2014 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 599316527/dae250f8470c909d4008 to your computer and use it in GitHub Desktop.
Save 599316527/dae250f8470c909d4008 to your computer and use it in GitHub Desktop.
Smart Date Formater
/**
* Format Date smartly
* @param {stirng} pubdate General date
* @return {string}
*/
function formatDate(pubdate) {
var pd = new Date(pubdate);
var td = new Date();
var diff = (td.getTime() - pd.getTime()) / 1000;
if (diff / 3600 / 24 > 99) {
return (pd.getMonth() + 1) + '月' + pd.getDate() + '日';
} else if (diff / 3600 / 24 >= 1) {
return Math.round(diff / 3600 / 24) + '天前';
} else if (diff / 3600 >= 1) {
return Math.round(diff / 3600) + '小时前';
} else if (diff / 60 >= 1) {
return Math.round(diff / 60) + '分钟前';
} else {
return Math.round(diff) + '秒前';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment