js 設置時間的顯示格式


/*
*設置顯示時間格式
*@param: 原始時間 yyyy-MM-dd HH:mm:ss
*/
function SetDisTime(oldTime) {
var nowDate = new Date();
var oldDate = new Date(oldTime);
var oldyear = oldDate.getFullYear();
var oldmonth = oldDate.getMonth() + 1; //js從0開始取
var oldday = oldDate.getDate();
var oldhour = oldDate.getHours();
var oldminutes = oldDate.getMinutes();
var oldsecond = oldDate.getSeconds();
var oldminutesText, oldsecondText;
oldminutesText = oldminutes < 10 ? "0" + oldminutes : oldminutes;
oldsecondText = oldsecond < 10 ? "0" + oldsecond : oldsecond;
var disPlayTime;
//判斷是否為今年
if (nowDate.getFullYear() != oldyear) {
disPlayTime = oldyear + "" + oldmonth + "" + oldday + "" + oldhour + ":" + oldminutesText;
return disPlayTime;
}
//判斷是否為本月
if (nowDate.getMonth() + 1 != oldmonth) {
disPlayTime = oldmonth + "" + oldday + "" + oldhour + ":" + oldminutesText;
return disPlayTime;
}
//判斷是否為今日
if (nowDate.getDate() != oldday) {
disPlayTime = oldmonth + "" + oldday + "" + oldhour + ":" + oldminutesText;
return disPlayTime;
}
else {

//判斷小時是否一致
if (nowDate.getHours() != oldhour) {
disPlayTime = "今日 " + oldhour + ":" + oldminutesText;
}
else {
//判斷分鍾是否一致
if (nowDate.getMinutes() != oldminutes) {
disPlayTime = Math.abs(oldminutes - nowDate.getMinutes()) + " 分鍾前";
}
else {
disPlayTime = Math.abs(oldsecond - nowDate.getSeconds()) + " 秒前";
}
}
}
return disPlayTime;

}

在頁面加載的時候調用以上方法:

$(function () {
$(".recTimes").each(function (indexs, controls) {
$(controls).html(SetDisTime($(controls).html()));
});
});

.recTimes是給時間元素的一個類名。
 

PS:我的淘寶店鋪新開業,經營各種桌游,棋牌,希望大伙兒能來看看!http://201314yes.taobao.com/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM