zoukankan      html  css  js  c++  java
  • javascript时间处理

    1、将一般格式时间转换为时间戳:

    var systime = "2018年04月28日 16:01:09";
    systime = systime.replace('年', "/").replace('月', "/").replace('日', '');
    systime = new Date(systime).getTime();
    

    2、将毫秒数转换为时分秒:

    var runtime = 6*60*60*1000;
    var day = Math.floor(runtime/(24*60*60*1000));    
    var h = Math.floor((runtime%(24*60*60*1000))/(60*60*1000));    
    var m = Math.floor(((runtime%(24*60*60*1000))%(60*60*1000))/(60*1000));
    var s = Math.floor((((runtime%(24*60*60*1000))%(60*60*1000))%(60*1000))/1000);

    3、动态显示获取到的系统时间和时长:

    <div id="serviceTime"></div>
        var serviceTime = document.getElementById("serviceTime");
        console.log("========【获取系统时间和时长】模块开始========");
        $.ajax({
            type: "POST",
            url: "/monitor/monitoring/nodes_info/",
            data: {},
            dataType: 'json',
            timeout: 1000 * 60,
            success: function(obj){
                if(obj){
                    //解析当前时间
                    var systime = obj.sysinfo.systime;//获取当前时间:"2018年04月28日 16:01:09"
                    systime = systime.replace('年', "/").replace('月', "/").replace('日', '');
                    systime = new Date(systime).getTime();
                    console.log("当前时间时间戳:");
                    console.log(systime);
                    //解析时长
                    var runtime = obj.sysinfo.runtime;//获取当前时长:"13天7小时58分17秒"
                    var indexOfDay = runtime.indexOf("天");
                    var indexOfHour1 = runtime.indexOf("小");
                    var indexOfHour2 = runtime.indexOf("时");
                    var indexOfMinutes = runtime.indexOf("分");
                    var indexOfSecond = runtime.indexOf("秒");
                    var runtimeDay = runtime.substring(0, indexOfDay); 
                    var runtimeHour = runtime.substring(indexOfDay+1, indexOfHour1); 
                    var runtimeMinutes = runtime.substring(indexOfHour2+1, indexOfMinutes); 
                    var runtimeSecond = runtime.substring(indexOfMinutes+1, indexOfSecond); 
                    runtime = runtimeDay*24*60*60*1000 + runtimeHour*60*60*1000 + runtimeMinutes*60*1000 + runtimeSecond*1000;
                    console.log("当前时长毫秒数:");
                    console.log(runtime);
                    
                    setInterval(function(){
                        var time = new Date(systime);
                        var year = time.getFullYear();
                        var month = time.getMonth() + 1;
                        var date = time.getDate();
                        if (date >= 0 && date <= 9) {
                            date = "0" + date;
                        }
                        var hour = time.getHours();
                        if (hour >= 0 && hour <= 9) {
                            hour = "0" + hour;
                        }
                        var minutes = time.getMinutes();
                        if (minutes >= 0 && minutes <= 9) {
                            minutes = "0" + minutes;
                        }
                        var seconds= time.getSeconds();
                        if (seconds >= 0 && seconds <= 9) {
                            seconds = "0" + seconds;
                        }
                        
                         var day = Math.floor(runtime/(24*60*60*1000));    
                         var h = Math.floor((runtime%(24*60*60*1000))/(60*60*1000));    
                         var m = Math.floor(((runtime%(24*60*60*1000))%(60*60*1000))/(60*1000));
                         var s = Math.floor((((runtime%(24*60*60*1000))%(60*60*1000))%(60*1000))/1000);
    
                        serviceTime.innerHTML = "运行时长:" + day + "天" + h + "时" + m + "分" + s + "秒 "+ " 系统时间:" + year + "年" + month + "月" + date + "日 " + hour + ":" + minutes + ":" + seconds;
                        systime += 1000;
                        runtime += 1000;
                    }, 1000);
                }else{
                    toastr.error("获取系统时间和时长时返回数据为空!", "警告");
                    console.log("获取系统时间和时长时返回数据为空!");
                }
                
            },
            error: function(msg){
                toastr.error("获取系统时间和时长时系统错误!", "警告");
                console.log("获取系统时间和时长时系统错误!");
                console.log(msg);
            }
        });
  • 相关阅读:
    网页中的默认按钮
    心动不如行动
    周日骑行广州大学城
    买单车别买重车
    今晚好无聊
    在自行车论坛看到的有趣帖子
    php zend framework 生成 pdf 出现中文乱码
    FPDI Import existing PDF documents into FPDF
    PHP 哈希表,关联数组,遍历方法大全
    zend framework 如何多表查询
  • 原文地址:https://www.cnblogs.com/samve/p/10023195.html
Copyright © 2011-2022 走看看