jenney.qiu

导航

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/

posted on 2012-03-23 10:52  jenney.qiu  阅读(9334)  评论(1编辑  收藏  举报