首页 > Web开发 > 详细

20150117_js_设置时间的显示格式

时间:2015-01-17 17:43:41      阅读:254      评论:0      收藏:0      [点我收藏+]

/*
*设置显示时间格式
*@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;

}

20150117_js_设置时间的显示格式

原文:http://www.cnblogs.com/wangfuyou/p/4230626.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!