给定时间和当前时间的时间差

给定时间,可以用 ("xx"-"xx"-"xx" "HH":"MM":"SS")的格式转化成时间格式字符串,

用Date.parse()转化为时间戳(含有毫秒,XX.getTime()或者XX.valueof()不计算毫秒),计算差值,然后转换。

举例:

     var year = document.getElementById('year-select-sub').textContent;
        var month = document.getElementById('month-select-sub').textContent;
        var day = document.getElementById('day-select-sub').textContent;
        var hour = document.getElementById('hour-select-sub').textContent;
        var minute = document.getElementById('minute-select-sub').textContent;
        var second = document.getElementById('second-select-sub').textContent;
        var old_time = year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second;
        var now_time,time,differ,str;
        now_time = new Date();
        old_time = Date.parse(old_time);
        now_time = Date.parse(now_time);
        differ = now_time-old_time;
        if(differ >= 0){
            str =  '已经过去';
        }else{
            str = '还有';
        }
        differ = Math.abs(differ);
        time = timeStamp(differ/1000);
        document.getElementById('differ').innerHTML = str+time;

 

<script>
    function timeStamp( second_time ){
        var time = parseInt(second_time) + "秒";

        if( parseInt(second_time )> 60){
            var second = parseInt(second_time) % 60;
            var min = parseInt(second_time / 60);
            time = min + "分" + second + "秒";

            if( min > 60 ){
                min = parseInt(second_time / 60) % 60;
                var hour = parseInt( parseInt(second_time / 60) /60 );
                time = hour + "小时" + min + "分" + second + "秒";

                if( hour > 24 ){
                    hour = parseInt( parseInt(second_time / 60) /60 ) % 24;
                    var day = parseInt( parseInt( parseInt(second_time / 60) /60 ) / 24 );
                    time = day + "天" + hour + "小时" + min + "分" + second + "秒";
                }
            }
        }

        return time;
    }
</script>

 

posted @ 2018-06-20 10:53  万里冰封  阅读(339)  评论(0编辑  收藏  举报