20150117_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;

}

 

posted @ 2015-01-17 16:42  Twang  阅读(330)  评论(0编辑  收藏  举报