Created
August 24, 2014 10:20
-
-
Save 599316527/dae250f8470c909d4008 to your computer and use it in GitHub Desktop.
Smart Date Formater
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
/** | |
* 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