JS 获取两个时间相差的天 小时 分钟 秒 自动判断输出

        //获取diff
        function getdiff(t1, t2) {
            if (t1 == t2) return "-";
            if (t1 == "" || t2 == "") return "-";
            var second = Math.abs((new Date(t1) - new Date(t2)) / 1000);
            var days = Math.floor(second / (24 * 3600));
            var hour = Math.floor((second - days * 24 * 3600) / 3600);
            var min = Math.floor((second - days * 24 * 3600 - hour * 3600) / 60);
            var s = second - days * 24 * 3600 - hour * 3600 - min * 60;
            return (days > 0 ? days + "天" : "") + (hour > 0 ? hour + "小时" : "") + (min > 0 ? min + "分钟" : "") + (s > 0 ? s + "秒" : "");
        }

 

posted @ 2017-09-22 17:00 araki 阅读(...) 评论(...) 编辑 收藏