zoukankan      html  css  js  c++  java
  • x秒前

    /**
     * 格式化时间,1秒前,1分钟前 今天 10:11 昨天 10:11 9月11日 10:10
     * @param {Date} date 时间对象
     * @param {Date} currentDate 对比时间
     * @return {String} 格式化后的时间内容
     */
    var shortTime = function(date, currentDate) {
        var now = currentDate,
            // 今日秒数
            todaySec = now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds(),
            secDiff = Math.floor( (now.getTime() - date.getTime()) / 1000 );
        if(secDiff < 60) {
            secDiff = secDiff < 1 ? 1 : secDiff;
            return parseInt(secDiff) + '秒前';
        }
        if(secDiff <= 3600) {
            return parseInt(secDiff / 60) + "分钟之前";
        }
        var h = ("0" + date.getHours()).slice(-2),
            m = ("0" + date.getMinutes()).slice(-2), 
            d = date.getDate();
        return ( now.getDate() == d && secDiff < 86400 ) ?  ( "今天 " + h + ":" + m ) : 
               ( secDiff >= todaySec && ( secDiff <= 86400 + todaySec ) ) ? ( "昨天 " + h + ":" + m ) :
               ( (date.getMonth() + 1) + "月" + d + "日 " +  h + ":" + m );
    };
    
    //但一般我们会拿到一个"xxxx-xx-xx xx:xx:xx"的"发布时间",要相对当前时间格式化
    var shortTime = function(date, currentDate) {
        var pubtimeThis = date.split(" ");
        var temp_a = pubtimeThis[0].split("-");
        var temp_b = pubtimeThis[1].split(":");
        date = new Date(temp_a[0], temp_a[1] - 1, temp_a[2], temp_b[0], temp_b[1]);
    
        var now = currentDate || (new Date()),
            // 今日秒数
            todaySec = now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds(),
            secDiff = Math.floor( (now.getTime() - date.getTime()) / 1000 );
        if(secDiff < 60) {
            secDiff = secDiff < 1 ? 1 : secDiff;
            return parseInt(secDiff) + '秒前';
        }
        if(secDiff <= 3600) {
            return parseInt(secDiff / 60) + "分钟之前";
        }
        var h = ("0" + date.getHours()).slice(-2),
            m = ("0" + date.getMinutes()).slice(-2), 
            d = date.getDate();
        return  ( now.getDate() == d && secDiff < 86400 ) ?  ( "今天 " + h + ":" + m ) :
                ( secDiff >= todaySec && ( secDiff <= 86400 + todaySec ) ) ?  ( "昨天 " + h + ":" + m ) : 
                ( (date.getMonth() + 1) + "月" + d + "日 " +  h + ":" + m );
    };
    

      

  • 相关阅读:
    用WinForm写的员工考勤项目!!!!!!
    洛谷P1892《[BOI2003]团伙》
    洛谷P1821《[USACO07FEB]银牛派对Silver Cow Party》
    洛谷P1149《火柴棒等式》
    2017 国庆清北刷题冲刺班《角谷猜想》
    洛谷P2330《[SCOI2005]繁忙的都市》
    洛谷P1955《[NOI2015]程序自动分析》
    洛谷P1536《村村通》
    Windows 10 体验记
    洛谷P1102《A-B数对》
  • 原文地址:https://www.cnblogs.com/frostbelt/p/2990615.html
Copyright © 2011-2022 走看看