zoukankan      html  css  js  c++  java
  • 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;
    
    }
  • 相关阅读:
    为什么很多程序员都选择跳槽?
    程序员牛人跳槽
    批处理学习教程
    linux操作命令
    apache配置访问限制
    不常见使用的css
    input中的内容改变时触发的事件
    order by 特殊排序技巧
    CSS设置input placeholder文本的样式
    GoodUI:页面布局的技巧和设计理念
  • 原文地址:https://www.cnblogs.com/wangfuyou/p/4230626.html
Copyright © 2011-2022 走看看