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;

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。