zoukankan      html  css  js  c++  java
  • JS时间处理,获取天时分秒。以及浏览器出现的不兼容问题

    //获取时间的天,小时,分钟,秒

    function ToTime(second) {
        second = second / 1000;
        var result
        var s = second % 60;
        var m = parseInt(second / 60) % 60;
        var h = parseInt(second / 3600) % 24;
        var d = parseInt(second / (3600 * 24));
        if (d != 0) {
            result = d + "" + h + "小时" + m + "分钟" + s + "";
        }
        else if (h != 0) {
            result = h + "小时" + m + "分钟" + s + "";
        }
        else if (m != 0) {
            result = m + "分钟" + s + "";
        }
        else {
            result = s + "";
        }
        return result;
    }

    //根据参数获取两个时间差的天时分秒

     1 function GetTime(a, b, type) {
     2     type = type.toUpperCase();  //将参数变成大写字母
     3     var date1 = new Date(a);    //开始时间
     4     var date2 = new Date(b);     //结束时间
     5     var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数
     6     var result = "-1";
     7     //计算出相差天数
     8     if (type == "D") result = Math.floor(date3 / (24 * 3600 * 1000));
     9     //计算出小时数 
    10     //if (type == "H") result = Math.floor(date3 % (24 * 3600 * 1000) / (3600 * 1000));
    11     if (type == "H") result = Math.floor(date3 / (3600 * 1000));
    12     //计算相差分钟数
    13     //if (type == "M") result = Math.floor(date3 % (24 * 3600 * 1000) % (3600 * 1000) / (60 * 1000));
    14     if (type == "M") result = Math.floor(date3 / (60 * 1000));
    15     //计算相差秒数
    16     //if (type == "S") result = Math.floor(date3 % (24 * 3600 * 1000) % (3600 * 1000) % (60 * 1000) / 1000);
    17     if (type == "S") result = Math.floor(date3 / 1000);
    18     return result;
    19 };

     时间兼容处理:

      如果a,b参数的格式为:“2017-03-21”,需要将格式转换成“2017/03/21”的。如果不转换的话,new Date(a)得到的数据是“NAN”。

  • 相关阅读:
    移动运营四:如何优化页面布局
    移动运营三:如何进行场景洞察
    移动运营二:如何更加了解你的用户
    移动运营一:如何进行运营效果分析
    操作系统的系统调用
    从图灵机到操作系统的启动
    伸展树(splay tree)
    AVL树
    二叉搜索树
    表达式树
  • 原文地址:https://www.cnblogs.com/siaslfslovewp/p/6201949.html
Copyright © 2011-2022 走看看